当前位置:网站首页>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());
}
}
边栏推荐
- 抽象问题方法论
- How to choose the right oscilloscope probe in different scenarios
- webrtc学习--webrtc源码获取
- Flutter开发:报错The following assertion was thrown resolving an image codec:Unable to…的解决方法
- 十年架构五年生活-07 年轻气盛的蜕变
- 矛盾的地方
- 如何成为一名合格的 DBA?看看“老油条”们怎么说
- 【论文笔记】Prototypical Contrast Adaptation for Domain Adaptive Semantic Segmentation
- JS获取简单当前时间的年、月、日、时间等
- I have a dream for Career .
猜你喜欢

ctf-pikachu-file_inclusion

线程(下):读写者模型\环形队列\线程池

【论文笔记】Prototypical Contrast Adaptation for Domain Adaptive Semantic Segmentation

2022年T电梯修理考试题及模拟考试

LeetCode·124.二叉树中的最大路径和·递归

阿笑家的黄桃

How Current Probes Set Oscilloscope Parameters

canvas 画布绘制时钟

虚假新闻检测论文阅读(七):A temporal ensembling based semi-supervised ConvNet for the detection of fake news

EasyGBS连接mysql数据库提示“can’t connect to mysql server”,该如何解决?
随机推荐
tensorflow分词深度学习——影评预测
栈与队列 | 用栈实现队列 | 用队列实现栈 | 基础理论与代码原理
十年架构五年生活-06 离职的冲动
ECMAScript6 Proxy和Reflect 对象操作拦截以及自定义
leetcode每天5题-Day12
PHPCMS仿站从入门到精通,小白看这一套课程就够了
网络层与数据链路层
curl命令介绍
LeetCode 2369. 检查数组是否存在有效划分 动态规划
文献 | 关于心理活动符号学,你知道多少?
请教一下各位大佬。CDC社区中FlinkCDC2.2.0版本有说明支持的sqlserver版本 ,请
【u-boot】u-boot驱动模型分析(02)
Thread.sleep, Thread.yield 作用解释
2022 R2 transportable pressure vessel filling operation examination question bank simulation platform
如何成为一名合格的 DBA?看看“老油条”们怎么说
用 PySpark ML 构建机器学习模型
FPGA工程师面试试题集锦41~50
LeetCode·1413.逐步求和得到正数的最小值·贪心
什么是遗留代码:有效地处理遗留代码的8个小贴士
An article to master the entire JVM, JVM ultra-detailed analysis!!!