当前位置:网站首页>每日一题-字典
每日一题-字典
2022-08-05 05:17:00 【菜鸡程序媛】
目录
字母异位词分组
- 时间:0801
- 题目
给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母通常恰好只用一次。
- 思路
- 属于异位词组的单词,按照字典顺序排序后就是一个单词,比如abc、cba,排序后都是abc
- 所以将abc当作key,value拼接「按照字典排序后」等于“abc”的单词们,有几个这样的key,就有几组不同的异位词组
- 代码
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> res = new ArrayList<>();
if(strs == null || strs.length == 0)
return res;
Map<String, List<String>> map = new HashMap<>();
for(String str : strs){
char[] ch = str.toCharArray();
Arrays.sort(ch);
String newStr = new String(ch);
if(map.containsKey(newStr)){
List<String> list = map.get(newStr);
list.add(str);
map.put(newStr, list);
}else{
List<String> list = new LinkedList<>();
list.add(str);
map.put(newStr, list);
}
}
for(Map.Entry<String, List<String>> entry : map.entrySet()){
res.add(entry.getValue());
}
return res;
}
}边栏推荐
猜你喜欢

5G中切片网络的核心技术FlexE

盘点关于发顶会顶刊论文,你需要知道写作上的这些事情!

Detailed explanation of BroadCast Receiver (broadcast)

【Multisim仿真】直流稳压电源设计报告

多边形等分

MySQL主从复制—有手就能学会的MySQL集群搭建教程
![[Database and SQL study notes] 9. (T-SQL language) Define variables, advanced queries, process control (conditions, loops, etc.)](/img/7e/566bfa17c5b138d1f909185721c735.png)
[Database and SQL study notes] 9. (T-SQL language) Define variables, advanced queries, process control (conditions, loops, etc.)

八、请求处理之自定义类型参数绑定原理

LeetCode刷题之第55题

哥廷根大学提出CLIPSeg,能同时作三个分割任务的模型
随机推荐
六、请求处理—获取请求参数系列注解是怎样工作的?
常用 crud 的思考和设计
网络信息安全运营方法论 (下)
Comparison and summary of Tensorflow2 and Pytorch in terms of basic operations of tensor Tensor
C语言入门笔记 —— 分支与循环
[Intensive reading of the paper] R-CNN's Bounding box regression problem is detailed
电子产品量产工具(3)- 文字系统实现
LeetCode刷题之第746题
WCH系列芯片CoreMark跑分
You should write like this
(C语言)strlen、strcpy、strcat、strcmp、strstr函数的模拟实现
【Over 15】A week of learning lstm
【shell编程】第三章:函数
【22李宏毅机器学习】课程大纲概述
(C语言)动态内存管理
OSPF网络类型
【Multisim仿真】直流稳压电源设计报告
电子产品量产工具(1)- 显示系统实现
It turns out that the MAE proposed by He Yuming is still a kind of data enhancement
CVPR 2022 |节省70%的显存,训练速度提高2倍