当前位置:网站首页>每日一题-字典
每日一题-字典
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;
}
}边栏推荐
- You should write like this
- 电子产品量产工具(4)-UI系统实现
- [Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)
- 「实用」运维新手一定不能错过的17 个技巧
- LeetCode刷题之第416题
- IT系统运行维护方法及策略
- 【论文阅读-表情捕捉】High-quality Real Time Facial Capture Based on Single Camera
- LeetCode刷题之第61题
- IJCAI 2022|边界引导的伪装目标检测模型BGNet
- 网络信息安全运营方法论 (上)
猜你喜欢

五、请求处理—Rest映射是怎样实现的?

读论文-Cycle GAN

A deep learning code base for Xiaobai, one line of code implements 30+ attention mechanisms.

电子产品量产工具(3)- 文字系统实现

函数在开发环境中的应用(简易实例)

Facial Motion Capture 调研

The University of Göttingen proposed CLIPSeg, a model that can perform three segmentation tasks at the same time

Machine Learning (1) - Machine Learning Fundamentals

【ts】typescript高阶:模版字面量类型

【UiPath2022+C#】UiPath变量和参数
随机推荐
LeetCode刷题之第74题
(C语言)计算结构体大小——结构体内存对齐
【论文阅读-表情捕捉】ExpNet: Landmark-Free, Deep, 3D Facial Expressions
原型版本管理
LeetCode刷题之第33题
【ts】typescript高阶:分布式条件类型
Facial Motion Capture 调研
电子产品量产工具(1)- 显示系统实现
多边形等分
【ts】typescript高阶:模版字面量类型
原来何恺明提出的MAE还是一种数据增强
函数在开发环境中的应用(简易实例)
【shell编程】第三章:函数
It turns out that the MAE proposed by He Yuming is still a kind of data enhancement
数控直流电源
LeetCode刷题之第129题
Redis设计与实现(第三部分):多机数据库的实现
基于STM32F407的一个温度传感器报警系统(用的是DS18B20温度传感器,4针0.96寸OLED显示屏,并且附带日期显示)
1004 成绩排名 (20 分)
单变量线性回归