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());
    }
}
相关推荐
倾心琴心1 分钟前
【agent辅助pcb routing coding学习】实践7 length matching 算法学习
学习·算法·agent·pcb·routing
y = xⁿ7 分钟前
【LeetCodehot100】T114:二叉树展开为链表 T105:从前序与中序遍历构造二叉树
java·算法·链表
灰色小旋风7 分钟前
力扣20有效的括号(C++)
c++·算法·leetcode·职场和发展
逆境不可逃11 分钟前
LeetCode 热题 100 之 160. 相交链表 206. 反转链表 234. 回文链表 141. 环形链表 142. 环形链表 II
算法·leetcode·链表
CoovallyAIHub21 分钟前
AAAI 2026 | 华中科大联合清华等提出Anomagic:跨模态提示零样本异常生成+万级AnomVerse数据集(附代码)
深度学习·算法·计算机视觉
博界IT精灵34 分钟前
王道书3.4.3:特殊矩阵的压缩存储
数据结构·考研·矩阵
npupengsir34 分钟前
nano vllm代码详解
人工智能·算法·vllm
m0_5698814738 分钟前
C++中的组合模式高级应用
开发语言·c++·算法
m0_7301151142 分钟前
高性能计算负载均衡
开发语言·c++·算法
灰色小旋风1 小时前
力扣19删除链表的倒数第N个结点(C++)
c++·算法·leetcode·链表