码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] 49. Group Anagrams 分组变位词

时间:2018-03-10 11:59:25      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:lists   end   rar   char   数组   values   pos   post   together   

Given an array of strings, group anagrams together.

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

[
  ["ate", "eat","tea"],
  ["nat","tan"],
  ["bat"]
] 

Note: All inputs will be in lower-case.

给一个含字符串的数组,字符串是变位词的分在一组,返回所有分组。变位词就是一个单词通过改变单词当中的字母顺序变成另外一个单词。

Java:

public List<List<String>> groupAnagrams(String[] strs){  
    List<List<String>> lists=new ArrayList<List<String>>();  
    Map<String,List<String>> map=new HashMap<String,List<String>>();  
    for(String str:strs){  
        char[] chs=str.toCharArray();  
        Arrays.sort(chs);  
        String tmp=new String(chs);  
        if(map.containsKey(tmp))  
            map.get(tmp).add(str);  
        else{  
            List<String> list=new ArrayList<String>();  
            list.add(str);  
            map.put(tmp, list);  
        }  
    }  
    for(String str:map.keySet()){  
        lists.add(map.get(str));  
    }  
    return lists;  
}

Java:

public class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (String s : strs) {
            char[] sc = s.toCharArray();
            Arrays.sort(sc);
            String key = String.valueOf(sc);
            map.putIfAbsent(key, new ArrayList<>());
            map.get(key).add(s);
        }
        return new ArrayList<>(map.values());
    }
}

Python:HashMap

class Solution(object):
    def groupAnagrams(self, strs):
        anagrams_map, result = collections.defaultdict(list), []

        for s in strs:
            sorted_str = ("").join(sorted(s))
            anagrams_map[sorted_str].append(s)

        for anagram in anagrams_map.values():
            anagram.sort()
            result.append(anagram)
        return result

C++:

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        vector<vector<string>> res;
        unordered_map<string, vector<string>> m;
        for (string str : strs) {
            string t = str;
            sort(t.begin(), t.end());
            m[t].push_back(str);
        }
        for (auto a : m) {
            res.push_back(a.second);
        }
        return res;
    }
};

C++:Array

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        vector<vector<string>> res;
        unordered_map<string, vector<string>> m;
        for (string str : strs) {
            vector<int> cnt(26, 0);
            string t = "";
            for (char c : str) ++cnt[c - ‘a‘];
            for (int d : cnt) t += to_string(d) + "/";
            m[t].push_back(str);
        }
        for (auto a : m) {
            res.push_back(a.second);
        }
        return res;
    }
};

  

  

  

 

 

 

[LeetCode] 49. Group Anagrams 分组变位词

标签:lists   end   rar   char   数组   values   pos   post   together   

原文地址:https://www.cnblogs.com/lightwindy/p/8537363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!