当前位置:网站首页>The sword refers to Offer 033. Variation array
The sword refers to Offer 033. Variation array
2022-08-10 05:03:00 【HotRabbit.】
题目
给定一个字符串数组 strs ,将 变位词 组合在一起. 可以按任意顺序返回结果列表.
**注意:**若两个字符串中每个字符出现的次数都相同,则称它们互为变位词.
示例 1:
输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
输出: [["bat"],["nat","tan"],["ate","eat","tea"]]
示例 2:
输入: strs = [""]
输出: [[""]]
示例 3:
输入: strs = ["a"]
输出: [["a"]]
提示:
1 <= strs.length <= 1040 <= strs[i].length <= 100strs[i]仅包含小写字母
注意:本题与主站 49 题相同: https://leetcode-cn.com/problems/group-anagrams/
Related Topics
- 数组
- 哈希表
- 字符串
- 排序
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/sfvd7V
著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处.
思路
将每个str 转为 char 数组,判断 map 是否存在这个 key.存在,value (List)add this string to.不存在,新建一个空的 List 集合.
题解
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String,List<String>> map = new HashMap<String,List<String>>();
for (String str : strs) {
char[] array = str.toCharArray();
Arrays.sort(array);
String key = new String(array);
List<String> list = map.getOrDefault(key,new ArrayList<String>());
list.add(str);
map.put(key,list);
}
return new ArrayList<List<String>>(map.values());
}
}
边栏推荐
- 虚假新闻检测论文阅读(七):A temporal ensembling based semi-supervised ConvNet for the detection of fake news
- 单页面应用
- About the problem that the mongodb driver count method of rust cannot be used with the near condition
- 关于rust的mongodb驱动count方法无法与near条件一同使用的问题
- curl命令介绍
- ORA-16018 异常处理记录
- JS gets the year, month, day, time, etc. of the simple current time
- 2022年T电梯修理考试题及模拟考试
- 2022年R2移动式压力容器充装考试题库模拟考试平台操作
- 线性模型中的高级特征选择技术——基于R
猜你喜欢
随机推荐
JS gets the year, month, day, time, etc. of the simple current time
Consulting cdc 2.0 for mysql does not execute flush with read lock. How to ensure bin
软考考生注意!2022年下半年报名详细流程来了!
SQL database field to append to main table
开发智能硬件过程中需要掌握的方法之经典
ctf-pikachu-file_inclusion
JVM内存模型
tensorflow分词深度学习——影评预测
How Current Probes Set Oscilloscope Parameters
PHPCMS仿站从入门到精通,小白看这一套课程就够了
【u-boot】u-boot驱动模型分析(02)
基于 EasyCV 复现 DETR 和 DAB-DETR,Object Query 的正确打开方式
【OpenCV图像处理4】算术与位运算
【裴蜀定理】CF1055C Lucky Days
MySQL simple tutorial
用 PySpark ML 构建机器学习模型
Stacks and Queues | Implementing Queues with Stacks | Implementing Stacks with Queues | Basic Theory and Code Principles
最新开源的面试笔记,天花板级别!
2022年A特种设备相关管理(电梯)考试模拟100题及答案
RK3568处理器体验小记









