Group Anagrams

要求:

輸入: ["eat", "tea", "tan", "ate", "nat", "bat"]輸出:
[
  ["ate","eat","tea"],
  ["nat","tan"],
  ["bat"]
]

所有輸入都爲小寫,輸出順序可以隨意。

第一次嘗試,主要用TreeMap的排序:

public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> result = new ArrayList<>();
        Map<String,List<String>> resultMap = new HashMap<>();

        List<String> tempList = new ArrayList<>();

        for (String string : strs) {
            char[] strArray=string.toCharArray();
            TreeMap<Character, Integer> map = new TreeMap<>();
            for(char c:strArray){
                if(map.get(c)!=null){
                    int num=map.get(c);
                    num++;
                    map.put(c,num);
                }else {
                    map.put(c,1);
                }
            }
            StringBuilder sb=new StringBuilder();
            for(Map.Entry<Character, Integer> entry:map.entrySet()){
                sb.append(entry.getKey()).append(entry.getValue());
            }
            String temStr=sb.toString();

            Boolean flag=true;
            for(String tmpMap:tempList){
                if(temStr.equals(tmpMap)){
                    List<String> listStr=resultMap.get(tmpMap);
                    listStr.add(string);
                    flag=false;
                    break;
                }
            }
            if(tempList.size()==0||flag){
                List list=new ArrayList<>();
                list.add(string);
                resultMap.put(temStr,list);
                tempList.add(temStr);
            }
        }

        for(List<String> t:resultMap.values()){
            result.add(t);
        }

        return result;
    }

結果較差:

第二次嘗試,主要用Set的特性,以及Collections.sort(),之後發現其實可以直接用Arrays.sort():

public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> result = new ArrayList<>();
        Map<String,List<String>> resultMap = new HashMap<>();

        Set<String> tempSet = new HashSet<>();

        for (String string : strs) {
            char[] strArray=string.toCharArray();
            List<Character> charList=new ArrayList<>();

            for(char c:strArray){
                charList.add(c);
            }
            Collections.sort(charList);
            String temStr= charList.toString();

            if(tempSet.add(temStr)){
                List list=new ArrayList<>();
                list.add(string);
                resultMap.put(temStr,list);
            }else {
                List<String> listStr=resultMap.get(temStr);
                listStr.add(string);
            }
        }

        for(List<String> t:resultMap.values()){
            result.add(t);
        }

        return result;
    }

結果一般,如下:

第三次嘗試,參考網上的思路,主要是把26個字母映射爲不同的素數,素數相乘是唯一的。代碼如下:

public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> result = new ArrayList<>();
        Map<Integer,Integer> dicMap = new HashMap<>();

        int[] tempArgs=new int[]{2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101};

        for(String str:strs){
            char[] strArray=str.toCharArray();
            int key=1;
            for(char c:strArray){
                key*=tempArgs[c-'a'];
            }
            Integer pos=dicMap.get(key);
            if(pos==null){
                dicMap.put(key,dicMap.size());
                List tempList=new ArrayList();
                tempList.add(str);
                result.add(tempList);
            }else {
                result.get(pos).add(str);
            }
        }

        return result;
    }

結果還是很Sex的:

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章