首页 > 其他 > 详细

Anagrams

时间:2014-02-06 16:56:58      阅读:415      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
 1 public class Solution {
 2     public ArrayList<String> anagrams(String[] strs) {
 3         int len = strs.length;
 4         HashMap<String,ArrayList<String>> hm = new HashMap<String,ArrayList<String>>();
 5         ArrayList<String> res = new ArrayList<String>();
 6         for(int i=0;i<len;i++){
 7             String sort = sort(strs[i]);
 8             if(hm.containsKey(sort)){
 9                 hm.get(sort).add(strs[i]);
10             }
11             else{
12                 ArrayList<String> temp = new ArrayList<String>();
13                 temp.add(strs[i]);
14                 hm.put(sort,temp);
15             }
16         }
17         Iterator it = hm.values().iterator();
18         while(it.hasNext()){
19             ArrayList<String> temp = (ArrayList<String>)it.next();
20             if(temp.size()>1){
21                res.addAll(temp);
22             }
23         }
24         return res;
25     }
26     public String sort(String s){
27         char [] c = s.toCharArray();
28         Arrays.sort(c);
29         return new String(c);
30     }
31 }
View Code

Anagrams

原文:http://www.cnblogs.com/krunning/p/3538751.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!