zoukankan      html  css  js  c++  java
  • Leetcode49.字母异位分组

    题目链接:49. 字母异位词分组

    思路:设置个哈希表,表键为排序后的字符串,表值为List,List中存放的字符串在排序后有相同的表键。

    代码:

    class Solution {
        public List<List<String>> groupAnagrams(String[] strs){
            Map<String, List<String>> map = new HashMap<>();
            for(String s : strs){
                char[] tc = s.toCharArray();
                Arrays.sort(tc);
                String t = String.valueOf(tc);
                if(!map.containsKey(t)){
                    map.put(t, new ArrayList<>());
                }
                map.get(t).add(s);
            }
            List<List<String>> res = new ArrayList<>();
            for(Map.Entry<String, List<String>> entry : map.entrySet()){
                res.add(entry.getValue());
            }
            return res;
        }
    }
    执行用时:7 ms, 在所有 Java 提交中击败了95.84%的用户
    内存消耗:41.4 MB, 在所有 Java 提交中击败了75.30%的用户
  • 相关阅读:
    如何删除.DS_Store文件?
    STL 技巧整理 7/22
    贪心
    防线
    bfs&dfs模板
    并查集学习总结
    二叉树相关代码
    区间DP学习笔记
    多算法模板整理
    防线题解
  • 原文地址:https://www.cnblogs.com/liuyongyu/p/14132723.html
Copyright © 2011-2022 走看看