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. LeetCode 49 字母分组

    2024-06-08 19:06:02       63 阅读
  2. LeetCode49字母分组

    2024-06-08 19:06:02       32 阅读
  3. leetcode_49.字母分组

    2024-06-08 19:06:02       38 阅读
  4. Leetcode 49. 字母分组

    2024-06-08 19:06:02       23 阅读
  5. leetcode49字母分组

    2024-06-08 19:06:02       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-08 19:06:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-08 19:06:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-08 19:06:02       82 阅读
  4. Python语言-面向对象

    2024-06-08 19:06:02       91 阅读

热门阅读

  1. git版本管理工具

    2024-06-08 19:06:02       31 阅读
  2. 动态语言的开源编译器汇总

    2024-06-08 19:06:02       26 阅读
  3. Oracle 收缩表高水位线

    2024-06-08 19:06:02       24 阅读
  4. Linux网络编程之select的理解

    2024-06-08 19:06:02       30 阅读
  5. MATLAB sort

    2024-06-08 19:06:02       26 阅读
  6. 2024-06-04 问AI: 介绍一下 Tensorflow 里面的 Keras

    2024-06-08 19:06:02       25 阅读
  7. spec文件是干嘛的?

    2024-06-08 19:06:02       30 阅读
  8. 11本AI人工智能相关电子书推荐(带下载地址)

    2024-06-08 19:06:02       31 阅读
  9. 深度学习 - PyTorch简介

    2024-06-08 19:06:02       23 阅读
  10. springAMQP(示例)

    2024-06-08 19:06:02       29 阅读