当前位置:网站首页>每日一题-字典
每日一题-字典
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;
}
}
边栏推荐
猜你喜欢
LeetCode刷题之第416题
1008 数组元素循环右移问题 (20 分)
C语言程序死循环问题解析——变量被修改
(C语言)动态内存管理
[Kaggle project actual combat record] Steps and ideas sharing of a picture classification project - taking leaf classification as an example (using Pytorch)
Thread handler handle IntentServvice handlerThread
伪RTOS-ProroThread在CH573芯片上的移植
11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT
网络ID,广播地址,掩码位数计算
网络信息安全运营方法论 (上)
随机推荐
CVPR 2020 - 频谱正则化
十、视图解析原理与源码分析
基于STM32F407的WIFI通信(使用的是ESP8266模块)
伪RTOS-ProroThread在CH573芯片上的移植
「实用」运维新手一定不能错过的17 个技巧
(oj)原地移除数组中所有的元素val、删除排序数组中的重复项、合并两个有序数组
CVPR 2022 |节省70%的显存,训练速度提高2倍
[Intensive reading of the paper] R-CNN's Bounding box regression problem is detailed
C语言查看大小端(纯代码)
十一、拦截器运行原理
You should write like this
[Database and SQL study notes] 10. (T-SQL language) functions, stored procedures, triggers
Jupyter notebook选择不同的Anaconda环境作为内核运行
CVPR最佳论文得主清华黄高团队提出首篇动态网络综述
将一句话的单词进行倒置(C语言纯代码)
LeetCode刷题之第61题
C语言入门笔记 —— 函数(1)
ECCV2022 | RU&谷歌提出用CLIP进行zero-shot目标检测!
LeetCode刷题之第33题
(C语言)计算结构体大小——结构体内存对齐