zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    【长沙集训】2017.9.11
    Miller-Rabin素数测试
    数论模板
    树状数组 区间修改+区间查询
    POJ 2104:K-th Number 整体二分
    Java中 VO、 PO、DO、DTO、 BO、 QO、DAO、POJO的概念
    redhat6.5 配置使用centos的yum源
    Failed to start firewalld.service: Unit firewalld.service is masked
    CentOS7安装iptables防火墙
    Linux下的tar压缩解压缩命令详解
  • 原文地址:https://www.cnblogs.com/Michael2397/p/8056444.html
Copyright © 2011-2022 走看看