Leetcode49 字母异位词分组

思路 : 字母异位词是字母排序不同,但字母总量相同的字符串,可以用一个排序后的String充当key,一个List收集对应该String的全部异位词。

主要API用法:

toCharArray : String转char类型数组

map.values() 取出Map的全部value值作为一个列表。

如果有疑问和更好的见解欢迎交流

复制代码
class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        //toCharArray
        //Arrays.sort()
        List<List<String>> res = new ArrayList<>();
        Map<String,List<String>> m = new HashMap<>();
        for(int i = 0;i<strs.length;++i){
            char[] cArray = strs[i].toCharArray();
            Arrays.sort(cArray);
            String tempString = new String(cArray);
            if(m.containsKey(tempString)){
                List<String> temp = m.get(tempString);
                temp.add(strs[i]);
                m.put(tempString,temp);
            }
            else{
                List<String> temp = new ArrayList<String>();
                temp.add(strs[i]);
                m.put(tempString, temp);
            }
        }
        return new ArrayList<List<String>>(m.values());
    }
}
相关推荐
人道领域7 分钟前
【LeetCode刷题日记】二叉树翻转:递归与迭代全解析
java·算法·leetcode
AI科技星11 分钟前
全域数学信息原本72分册(数学物理卷)
人工智能·算法·数学建模·数据挖掘·量子计算
进击的荆棘15 分钟前
递归、搜索与回溯——综合(上)
c++·算法·leetcode·深度优先·dfs
平凡但不平庸的码农8 小时前
Go Slice 详解
算法·golang
炸膛坦客10 小时前
嵌入式 - 数据结构与算法:(1-7)数据结构 - 顺序表和链表的对比
数据结构·链表
Jasmine_llq11 小时前
《B3867 [GESP202309 三级] 小杨的储蓄》
算法·循环遍历·数组累加(模拟)·索引定位·顺序输出
啦啦啦_999911 小时前
案例之 逻辑回归_电信用户流失预测
算法·机器学习·逻辑回归
风筝在晴天搁浅11 小时前
快手/字节 CodeTop LeetCode 415.字符串相加
算法·leetcode
hoiii18712 小时前
基于栅格法的机器人工作空间划分系统
数据结构·机器人
DragonnAi12 小时前
猫咪如厕检测与分类识别系统系列【十四】 项目结构重新整理-即将开源完整算法
算法·开源