Leetcode:336. Palindrome Pairs

这题一上来就会想到暴力破解,破解代码见http://blog.csdn.net/u012848330/article/details/51660542,超时

public class Solution {
    public List<List<Integer>> palindromePairs(String[] words) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        for(int i = 0; i < words.length; i++){
            for(int j = 0; j < words.length; j++){
                if(i != j && (words[i].length() == 0 || words[j].length() == 0 || words[i].charAt(0) == words[j].charAt(words[j].length()-1))){
                    if(isPalindrome(words[i]+words[j])){
                        List<Integer> list = new ArrayList<Integer>();
                        list.add(i);
                        list.add(j);
                        result.add(list);
                    }
                }
            }
        }
        
        return result;
    }
    
    private boolean isPalindrome(String s){
        int left = 0;
        int right = s.length()-1;
        while(left <= right){
            if(s.charAt(left) == s.charAt(right)){
                left++;
                right--;
            }else
                return false;
        }
        return true;
    }
}

Accept参考:https://discuss.leetcode.com/topic/40657/150-ms-45-lines-java-solution http://blog.csdn.net/u012848330/article/details/51660542

仿照着写出了自己的代码,发现有重复的

class Solution {
public List<List<Integer>> palindromePairs(String[] words) {
        List<List<Integer>> result = new ArrayList<>();
        if(words==null||words.length<2){
            return result;
        }
        Map<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 str1 = words[i].substring(0,j);
                String str2 = words[i].substring(j);
                if(isPalindrome(str1)){
                    Integer index = map.get(new StringBuilder(str2).reverse().toString());
                    if(index!=null&&index!=i){
                        List<Integer> list = new ArrayList<>();
                        list.add(index);
                        list.add(i);
                        result.add(list);
                    }
                }
                if(isPalindrome(str2)){
                    Integer index = map.get(new StringBuilder(str1).reverse().toString());
                    if(index!=null&&index!=i){
                        List<Integer> list = new ArrayList<>();
                        list.add(i);
                        list.add(index);
                        result.add(list);    
                    }
                }
            }
        }
        return result;
    }
    public Boolean isPalindrome(String str){
        int left = 0;
        int right = str.length()-1;
        while(left<=right){
            if(str.charAt(left)==str.charAt(right)){
                left++;
                right--;
            }
            else {
                return false;
            }
        }
        return true;
    }
}

最后修改:用集合来存储结果,最后把集合转化成list

public class Solution2 {
    public List<List<Integer>> palindromePairs(String[] words) {
        Set<List<Integer>> result = new HashSet<>();
    
        Map<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 str1 = words[i].substring(0,j);
                String str2 = words[i].substring(j);
                if(isPalindrome(str1)){
                    Integer index = map.get(new StringBuilder(str2).reverse().toString());
                    if(index!=null&&index!=i){
                        List<Integer> list = new ArrayList<>();
                        list.add(index);
                        list.add(i);
                        result.add(list);
                    }
                }
                if(isPalindrome(str2)){
                    Integer index = map.get(new StringBuilder(str1).reverse().toString());
                    if(index!=null&&index!=i){
                        List<Integer> list = new ArrayList<>();
                        list.add(i);
                        list.add(index);
                        result.add(list);    
                    }
                }
            }
        }
        List<List<Integer>> result1 = new ArrayList<>(result);
        return result1;
    }
    public Boolean isPalindrome(String str){
        int left = 0;
        int right = str.length()-1;
        while(left<=right){
            if(str.charAt(left)==str.charAt(right)){
                left++;
                right--;
            }
            else {
                return false;
            }
        }
        return true;
    }
}
原文地址:https://www.cnblogs.com/Michael2397/p/8056444.html