当前位置:网站首页>每日一题-字典
每日一题-字典
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;
}
}边栏推荐
猜你喜欢

MSRA proposes extreme masking model ExtreMA for learning instances and distributed visual representations

最简单的防抖节流理解法

ECCV2022 | RU & Google propose zero-shot object detection with CLIP!

网管日记:故障网络交换机快速替换方法

(C语言)计算结构体大小——结构体内存对齐

11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT

(C语言)动态内存管理

C语言—三子棋的实现

Machine Learning (1) - Machine Learning Fundamentals

CAN、CAN FD
随机推荐
(C语言)计算结构体大小——结构体内存对齐
十、视图解析原理与源码分析
对象比较
[Intensive reading of the paper] R-CNN's Bounding box regression problem is detailed
CVPR2020 - 自校准卷积
Facial Motion Capture 调研
CVPR最佳论文得主清华黄高团队提出首篇动态网络综述
(C语言)strlen、strcpy、strcat、strcmp、strstr函数的模拟实现
CVPR 2022 |节省70%的显存,训练速度提高2倍
发顶会顶刊论文,你应该这样写作
[Database and SQL study notes] 9. (T-SQL language) Define variables, advanced queries, process control (conditions, loops, etc.)
最简单的防抖节流理解法
神经网络也能像人类利用外围视觉一样观察图像
HuiFer 带你读懂 BeanFactory getBean 方法
【ts】typeScript高阶:any和unknown
【Shell编程】第一章:子串
C语言程序死循环问题解析——变量被修改
栈的应用——力扣 20.有效的括号
盘点关于发顶会顶刊论文,你需要知道写作上的这些事情!
IJCAI 2022|边界引导的伪装目标检测模型BGNet