美文网首页
给定不同单词组成的数组,俩俩拼接成回文,记录顺序索引

给定不同单词组成的数组,俩俩拼接成回文,记录顺序索引

作者: 敲一手烂代码 | 来源:发表于2017-05-20 17:47 被阅读9次
//    5.20
//    Given a list of unique words,
//    find all pairs of distinct indices (i, j) in the given list, 
//    so that the concatenation of the two words,
//    i.e. words[i] + words[j] is a palindrome.
//
//    Example 1:
//    Given words = ["bat", "tab", "cat"]
//    Return [[0, 1], [1, 0]]
//    The palindromes are ["battab", "tabbat"]
//    Example 2:
//    Given words = ["abcd", "dcba", "lls", "s", "sssll"]
//    Return [[0, 1], [1, 0], [3, 2], [2, 4]]
//    The palindromes are ["dcbaabcd", "abcddcba", "slls", "llssssll"]

先写一个超时版

public List<List<Integer>> palindromePairs(String[] words) { //Time Limit Exceeded
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        for (int i = 0; i < words.length; i++) {
            for (int j = 0; j < words.length; j++) {
                if (i != j && twoWordIntegrateIsPalindrome(words[i],words[j])) {
                    List<Integer> tempList = new ArrayList<Integer>(Arrays.asList(i,j));
                    list.add(tempList);
                }
            }
        }
        return list;
    }
    
    static boolean twoWordIntegrateIsPalindrome(String str1, String str2) {
        StringBuffer str = new StringBuffer(str1 + str2);
        return (str1 + str2).equals(str.reverse().toString());
    }

AC版

public List<List<Integer>> palindromePairs1(String[] words) { //Time Limit Exceeded
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        for (int i = 0; i < words.length; i++) {
            map.put(words[i], i);
        }
        for (int i = 0; i < words.length; i++) {
            for (int j = 0; j <= words[i].length(); j++) {
                String preString = words[i].substring(0,j);
                String laString = words[i].substring(j);
                if (isPalindrome(preString)) {
                    String tempString = new StringBuffer(laString).reverse().toString();
                    if (map.containsKey(tempString) && map.get(tempString) != i) {
                        List<Integer> tempList = new ArrayList<Integer>(Arrays.asList(map.get(tempString),i));
                        list.add(tempList);
                    }
                }
                if (isPalindrome(laString)) {
                    String tempString = new StringBuffer(preString).reverse().toString();
                    if (map.containsKey(tempString) && map.get(tempString) != i && laString.length() != 0) {
                        List<Integer> tempList = new ArrayList<Integer>(Arrays.asList(i,map.get(tempString)));
                        list.add(tempList);
                    }
                }
            }
        }
        return list;
    }
    
    static boolean isPalindrome(String str) {
        String str1 = new StringBuffer(str).reverse().toString();
        return str1.equals(str);
    }

相关文章

  • 给定不同单词组成的数组,俩俩拼接成回文,记录顺序索引

    先写一个超时版 AC版

  • 算法练习

    将字符串转化为数字,实现int()方法 回文数 俩数之和 给定一个整数数组 nums 和一个目标值 target,...

  • 回文数组 搜狐2018秋招笔试题

    对于一个给定的正整数组成的数组 a[] ,如果将 a 倒序后数字的排列与 a 完全相同,我们称这个数组为“回文”的...

  • 问题:求最小字符串拼接序列

    对于一个给定的字符串数组,请找到一种拼接顺序,使所有小字符串拼接成的大字符串是所有可能的拼接中字典序最小的。不同于...

  • 力扣 139 单词拆分

    题意:给定一个单词,和一个字典,判定字典中的单词能否拼接成给定的单词 思路:一维动态规划,详情见注释 思想:动态规...

  • 3_8拼接最小字典序

    对于一个给定的字符串数组,请找到一种拼接顺序,使所有小字符串拼接成的大字符串是所有可能的拼接中字典序最小的。 给定...

  • 拼接最小字典序

    题目 对于一个给定的字符串数组,请找到一种拼接顺序,使所有小字符串拼接成的大字符串是所有可能的拼接中字典序最小的。...

  • 算法(9) 拼接最小字符串

    描述对于一个给定的字符串数组,请找到一种拼接顺序,使所有小字符串拼接成的大字符串是所有可能的拼接中字典序最小的。给...

  • 俩不同的孩子

    今天要聊到这俩孩子几乎没有什么可比性。 。。。。。 胡宏宇,两年前我都教过他,当时印象还真是不深,没想到如今这孩子...

  • KMP

    kmp周期 最短回文串next数组可以得到前缀和后缀中相同的长度有多少,然后将回文串反转和不反转拼接起来

网友评论

      本文标题:给定不同单词组成的数组,俩俩拼接成回文,记录顺序索引

      本文链接:https://www.haomeiwen.com/subject/weupxxtx.html