leetcode 49. Group Anagrams

来源:互联网 发布:淘宝怎么编辑宝贝 编辑:程序博客网 时间:2024/05/23 02:01

Given an array of strings, group anagrams together.

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

[
[“ate”, “eat”,”tea”],
[“nat”,”tan”],
[“bat”]
]

本题就是寻找所有字符相等的字符串的集合,要注意字符可能重复,所以要使用List,不可以使用set,就是使用Map做映射,还是很简单的。

代码如下:

import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.List;import java.util.Map;public class Solution {    public List<List<String>> groupAnagrams(String[] strs)     {        //一开始是准备遍历比较的,但是发现使用map映射更加快捷方便        List<List<String>> res=new ArrayList<>();        //map的key值不可以是“”,所以这个要特殊处理        List<String> listForKong=new ArrayList<>();        //key值存储的是char的list,并且是排好序的        Map<List<Character>,List<String>> map=new HashMap<List<Character>, List<String>>();        //特殊情况处理        if(strs==null || strs.length<=0)            return res;        for(int i=0;i<strs.length;i++)        {               //特殊情况:“” 的处理            if(strs[i].equals(""))                listForKong.add(strs[i]);            else            {                //下面是构建char的list                List<Character> tmp=new ArrayList<>();                for(int j=0;j<strs[i].length();j++)                    tmp.add(strs[i].charAt(j));                //排序                tmp.sort(null);                //添加到map                if(map.containsKey(tmp))                    map.get(tmp).add(strs[i]);                else                {                    List<String> valueList=new ArrayList<>();                    valueList.add(strs[i]);                    map.put(tmp, valueList);                }            }        }        //map 的value就是我们要的结果        for(List<String> it:map.values())            res.add(it);            //针对“” 的特殊处理        if(listForKong.size()>0)            res.add(listForKong);        return res;     }}

下面是C++的答案,就是遍历求解

代码如下:

#include <iostream>#include <algorithm>#include <vector>#include <string>#include <map>using namespace std;class Solution {public:    vector<vector<string>> groupAnagrams(vector<string>& strs)     {        vector<vector<string>> res;        if (strs.size() <= 0)            return res;        map<vector<char>, vector<string>> mp;        vector<string> forNull;        for (int i = 0; i < strs.size(); i++)        {            string key = strs[i];            if (key == "")                forNull.push_back(key);            else            {                vector<char> one;                for (int j = 0; j < key.length(); j++)                    one.push_back(key[j]);                sort(one.begin(),one.end());                if (mp.find(one) == mp.end())                {                    vector<string> tt{ key };                    mp[one] = tt;                }                else                    mp[one].push_back(key);            }        }        if (forNull.size() > 0)            res.push_back(forNull);        map<vector<char>, vector<string>>::iterator it;        for (it = mp.begin(); it != mp.end(); it++)            res.push_back(it->second);        return res;    }};