首页 > 其他 > 详细

Group Anagrams

时间:2018-03-13 13:28:47      阅读:169      评论:0      收藏:0      [点我收藏+]

Given an array of strings, group anagrams together.

For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],
Return:

[
  ["ate", "eat","tea"],
  ["nat","tan"],
  ["bat"]
]
public List<List<String>> groupAnagrams(String[] strs) {
        Map<String,List<String>> map = new HashMap<>();
        for (String string:strs){
            char[] chars = string.toCharArray();
            Arrays.sort(chars);
            String value = String.valueOf(chars);
            if (!map.containsKey(value)){
                List<String> list = new ArrayList<>();
                map.put(value,list);
            }
            map.get(value).add(string);
        }
        List<List<String>> lists = new ArrayList<>();
        for (List<String> list:map.values()) {
            lists.add(list);
        }
        return lists;
    }

Group Anagrams

原文:https://www.cnblogs.com/bingo2-here/p/8555961.html

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