当前位置:网站首页>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 <= 104
0 <= strs[i].length <= 100
strs[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());
}
}
边栏推荐
- 2022 R2 transportable pressure vessel filling operation examination question bank simulation platform
- SQL Server query optimization
- 2022山东省安全员C证考试题及模拟考试
- openvino 安装(01)
- MySQL使用简单教程
- 重要转型升级
- 2022G3 Boiler Water Treatment Exam Mock 100 Questions and Mock Exam
- 盼他一切安好
- 解决“File has been changed outside the editor, reload?”提示
- flinksql怎么写redis的value只有最后一个字段?
猜你喜欢
随机推荐
LeetCode·1413.逐步求和得到正数的最小值·贪心
告诉你如何从keil工程知道使用了多少RAM和ROM空间
GP如何进行数据比对?
JS获取当前时间的年、月、日、时间等
LeetCode·301.删除无效的括号·BFS
mysql cdc (2.1.1)inital snapshot数据库的时候设置了5个并发度,se
webrtc学习--一对一通话
Promise原理及实现
【无标题】
【Web3 系列开发教程——创建你的第一个 NFT(7)】创建一个 NFT DApp,给你的 NFT 赋予属性,例如图片
栈与队列 | 有效的括号、删除字符串中的所有相邻元素、逆波兰表达式求值、滑动窗口的最大值、前K个高频元素 | leecode刷题笔记
When oracle cdc, set the parallelism to 2 and the number of slots to 1, and the final task has only one tm. Is it because oracle does not support concurrency
2022年R2移动式压力容器充装考试题库模拟考试平台操作
webrtc学习--webrtc桌面采集
线性模型中的高级特征选择技术——基于R
How cursors work in Pulsar
About the problem that the mongodb driver count method of rust cannot be used with the near condition
【OpenCV图像处理4】算术与位运算
【无标题】
2022 security officer C certificate test and simulation test in shandong province