当前位置:网站首页>剑指Offer 033.变位数组
剑指Offer 033.变位数组
2022-08-10 04:39: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)中添加这个字符串。不存在,新建一个空的 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());
}
}
边栏推荐
猜你喜欢
openvino 安装(01)
pdd.order.information.get拼多多订单详情接口代码对接教程
webrtc学习--websocket服务器(二) (web端播放h264)
What is the relationship between legal representative and shareholders?
智能锁控板的主要功能有哪些?如何使用?
How to choose the right oscilloscope probe in different scenarios
PID与ADRC
众昂矿业:萤石下游需求强劲
什么是SRM?有什么作用?在企业管理中能实现哪些功能?
线程(中):线程安全
随机推荐
众昂矿业:萤石下游需求强劲
2022/8/9
LeetCode·124.二叉树中的最大路径和·递归
【OpenCV图像处理5】图像的变换
西门子Step7和TIA软件“交叉引用”的使用
PID整定方法
JVM类加载机制
Ueditor编辑器任意文件上传漏洞
2022 R2 transportable pressure vessel filling operation examination question bank simulation platform
About the problem that the mongodb driver count method of rust cannot be used with the near condition
PHPCMS仿站从入门到精通,小白看这一套课程就够了
webrtc学习--一对一通话
法定代表人和股东是什么关系
Acwing 59. 把数字翻译成字符串 计数类DP
Unity实现UI的边缘检测和拖拽拉伸功能
使用 DatePicker 日期控件,发生 Prop being mutated: “placement“ 报错问题
互联网家装驶入深水区:谁在求变,又有谁在裸泳?
ZZULIOJ:1019: 公园门票
2022年P气瓶充装操作证考试题库及模拟考试
元宇宙 | 你能通过图灵测试吗?