首页 > 其他 > 详细

LeetCode Anagrams My solution

时间:2014-10-31 19:05:45      阅读:272      评论:0      收藏:0      [点我收藏+]

Anagrams

 

Given an array of strings, return all groups of strings that are anagrams.

Note: All inputs will be in lower-case.


public class Solution {
    public List<String> anagrams(String[] strs) {
        List<String> result = new ArrayList<String>();
        if (strs == null || strs.length == 0 ) {
            return result;
        }
     HashMap<String,Integer> hmaps = new HashMap<String,Integer>();
     
     for (int i = 0; i < strs.length; i++) {
         String curSort = sortString(strs[i]);
         if (hmaps.containsKey(curSort)) {
           hmaps.put(curSort, hmaps.get(curSort) + 1);   
         } else {
           hmaps.put(curSort, 1); 
         }
     }
     for(int i = 0; i < strs.length; i++) {
         if (hmaps.containsKey(sortString(strs[i])) && hmaps.get(sortString(strs[i])) > 1) {
             result.add(strs[i]);
         }
     }
     return result;
    }
    String sortString(String str) {
        char [] charArr = str.toCharArray();
        Arrays.sort(charArr);
        return Arrays.toString(charArr);
    }
}


LeetCode Anagrams My solution

原文:http://blog.csdn.net/aresgod/article/details/40659193

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