0
点赞
收藏
分享

微信扫一扫

LeetCode:49. 字母异位词分组

橙子好吃吗 2022-04-24 阅读 91
javaleetcode

问题描述(原题链接)

代码:

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        int index=0;
        HashMap<String,Integer> map = new HashMap<String,Integer>();
        List<List<String>> list = new ArrayList<List<String>>();
        int len = strs.length;
        for(int i=0;i<len;i++){
            char[] temp = strs[i].toCharArray();
            Arrays.sort(temp);
            if(map.containsKey(new String(temp))){
                int j = map.get(new String(temp));
                list.get(j).add(strs[i]);
            }else{
                map.put(new String(temp),list.size());
                List<String> temp2 = new ArrayList<String>();
                temp2.add(strs[i]);
                list.add(temp2);
            }
        }
        return list;
    }
}
举报

相关推荐

0 条评论