GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — JavaInterview/java/src/LeetCode/GroupAnagrams.java at master · damaohongtu/JavaInterview Expand file tree/ Copy pathGroupAnagrams.javaMore file actions 49 lines (45 loc) · 1.43 KB / Copy pathGroupAnagrams.javaFile metadata and controls49 lines (45 loc) · 1.43 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 package LeetCode; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class GroupAnagrams { public List<List<String>> groupAnagrams(String[] strs) { List<List<String>>result=new ArrayList<>(); HashMap<String,List<String>> res=new HashMap<>(); for (String s:strs){ List<String> t=getAnagrams(s); int flag=0; for(String tt:t){ if(res.keySet().contains(tt)){ res.get(tt).add(s); flag=1; break; } } if(flag==0){ List<String> l=new ArrayList<>(); l.add(s); res.put(s,l); } } result.addAll(res.values()); return result; } public List<String> getAnagrams(String s){ List<String> temp=new ArrayList<>(); for(int i=0;i<s.length();i++){ temp.add(s.substring(i)+s.substring(0,i)); } String s2=new StringBuffer(s).reverse().toString(); temp.add(s2); for(int i=0;i<s.length();i++){ temp.add(s.substring(i)+s.substring(0,i)); } return temp; } public static void main(String[] args){ GroupAnagrams groupAnagrams=new GroupAnagrams(); String [] test={"eat", "tea", "tan", "ate", "nat", "bat"}; System.out.println(groupAnagrams.groupAnagrams(test)); } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |