leetcode-49-字母異位詞分組(group anagrams)-java
阿新 • • 發佈:2018-11-13
題目及測試
package pid049; import java.util.List; /*字謎分組 給定一個字串陣列,將字母異位詞組合在一起。字母異位詞指字母相同,但排列不同的字串。 示例: 輸入: ["eat", "tea", "tan", "ate", "nat", "bat"], 輸出: [ ["ate","eat","tea"], ["natt","tatn"], ["bat"] ] 說明: 所有輸入均為小寫字母。 不考慮答案輸出的順序。 */ public class main { public static void main(String[] args) { String[][] testTable1 ={ {"eat", "tea", "tan", "ate", "nat", "bat"}, {"eatt", "ttea"}, {"erd", "tea"} }; for(int i=0;i<testTable1.length;i++){ test(testTable1[i]); } } private static void test(String[] ito) { Solution solution = new Solution(); int length=ito.length; for(int i=0;i<length;i++){ System.out.print( ito[i]+" "); } System.out.println(); long begin = System.currentTimeMillis(); List<List<String>> rtn=solution.groupAnagrams(ito);//執行程式 long end = System.currentTimeMillis(); for(int i=0;i<rtn.size();i++){ List<String> now=rtn.get(i); for(int j=0;j<now.size();j++){ System.out.print( now.get(j)+" "); } System.out.println(); } System.out.println(); System.out.println("耗時:" + (end - begin) + "ms"); System.out.println("-------------------"); } }
解法1(成功,826ms,超慢)
建立一個hashmap的列表,每個hashmap 以character為key,以integer為value
把每個字串,每個字元的數量存入其中
然後將hashmap的equals方法,逐個比對後加入
package pid049; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; public class Solution { public List<List<String>> groupAnagrams(String[] strs) { int length=strs.length; if(length==0){ return null; } List<HashMap<Character, Integer>> source=new ArrayList<>(); for(int i=0;i<length;i++){ HashMap<Character, Integer> now=new HashMap<>(); String nowStr=strs[i]; for(int j=0;j<nowStr.length();j++){ Character nowChar=nowStr.charAt(j); if(now.containsKey(nowChar)){ now.put(nowChar, now.get(nowChar)+1); } else{ now.put(nowChar, 1); } } source.add(now); } List<List<String>> result=new ArrayList<>(); List<String> sour=new ArrayList<>(Arrays.asList(strs)); boolean[] hasStr=new boolean[length]; for(int i=0;i<length;i++){ if(hasStr[i]==false){ List<String> nowResult=new ArrayList<>(); nowResult.add(strs[i]);//入字串 hasStr[i]=true; for(int j=i+1;j<length;j++){ if(hasStr[j]==false){ if(source.get(i).equals(source.get(j))){ nowResult.add(strs[j]);//入字串 hasStr[j]=true; } } } result.add(nowResult); } } return result; } }
解法2(別人的,21ms,超快)
採用HashMap<String,List<String>>,每次講讀入的字串在map中查詢(這裡需將讀入的字串轉化成陣列後用sort()來排列好)。
用sort後的字串作為key,與字串整理後一樣的字串集合的list作為value
在詳細的實現時,我們可以宣告一個HashMap<String, List<String>>,key是根據一個字串排序後得到的一個串,value是相同的anagram構成的列表。因為要求後面的結果是排序的,我們在後面將所有的value值都從Map裡取出來的時候,先對value排一下序再加入到最終的列表中即得到最終的結果。
public class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> list = new ArrayList<List<String>>();
int len = strs.length;
if(len<1) return list;
Map<String,List<String>> map = new HashMap<String,List<String>>();
String tmp = "";
for(int i=0;i<len;i++){
tmp = strs[i];
char[] arrayOfString = tmp.toCharArray();
Arrays.sort(arrayOfString);
tmp = new String(arrayOfString);
if(map.containsKey(tmp)){
map.get(tmp).add(strs[i]);
}else{
List<String> item = new ArrayList<String>();
item.add(strs[i]);
map.put(tmp, item);
}
}
for (List<String> value : map.values()) {
list.add(value);
}
return list;
}
}