当前位置:网站首页>49. 字母异位词分组:给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。 字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母通常恰好只用一次。
49. 字母异位词分组:给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。 字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母通常恰好只用一次。
2022-07-05 13:19:00 【?abc!】
题目描述
给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。
字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母通常恰好只用一次。
示例 1:
输入: strs = [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”]
输出: [[“bat”],[“nat”,“tan”],[“ate”,“eat”,“tea”]]
示例 2:
输入: strs = [“”]
输出: [[“”]]
示例 3:
输入: strs = [“a”]
输出: [[“a”]]
思路
从题目意思可知:
- 两个字符串互为字母异位词,需要
两个字符串包含的字母相同 - 因此对
两个字符串分别进行排序之后得到的字符串一定是相同的 - 使用哈希表存储每一组字母异位词,
哈希表的键为一组字母异位词的标志,哈希表的值为一组字母异位词列表 将排序之后的字符串作为哈希表的键
代码
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);
//将排序后的字符数组转为字符串作为map的键
String key = new String(array);
//map.getOrDefault(key,defaultValue):当Map集合中有这个key时,就使用这个key对应的value值,如果没有这个key就使用默认值defaultValue
List<String> list = map.getOrDefault(key,new ArrayList<String>());
//将当前字符串存入list集合中
list.add(str);
//将其存入map中
map.put(key,list);
}
return new ArrayList<List<String>>(map.values());
}
}
代码说明
注释见。。。
边栏推荐
- The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched
- “百度杯”CTF比赛 九月场,Web:SQL
- [深度学习论文笔记]使用多模态MR成像分割脑肿瘤的HNF-Netv2
- 程序员成长第八篇:做好测试工作
- Alibaba cloud SLB load balancing product basic concept and purchase process
- Actual combat simulation │ JWT login authentication
- Apicloud studio3 API management and debugging tutorial
- 数据湖(七):Iceberg概念及回顾什么是数据湖
- ASEMI整流桥HD06参数,HD06图片,HD06应用
- Overflow toolbar control in SAP ui5 view
猜你喜欢

Principle and configuration of RSTP protocol

About the single step debugging of whether SAP ui5 floating footer is displayed or not and the benefits of using SAP ui5

百度杯”CTF比赛 2017 二月场,Web:爆破-2

The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched

Solve Unicode decodeerror: 'GBK' codec can't decode byte 0xa2 in position 107

阿里云SLB负载均衡产品基本概念与购买流程

What is a network port

国际自动机工程师学会(SAE International)战略投资几何伙伴

UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe6 in position 76131: invalid continuation byt
![[深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接](/img/b6/f9da8a36167db10c9a92dabb166c81.png)
[深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接
随机推荐
Overflow toolbar control in SAP ui5 view
【Hot100】34. 在排序数组中查找元素的第一个和最后一个位置
一文详解ASCII码,Unicode与utf-8
MMSeg——Mutli-view时序数据检查与可视化
Realize the addition of all numbers between 1 and number
Reverse Polish notation
蜀天梦图×微言科技丨达梦图数据库朋友圈+1
Laravel document reading notes -mews/captcha use (verification code function)
Hiengine: comparable to the local cloud native memory database engine
Sorry, we can't open xxxxx Docx, because there is a problem with the content (repackaging problem)
CAN和CAN FD
[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
山东大学暑期实训一20220620
碎片化知识管理工具Memos
MSTP and eth trunk
jenkins安装
事务的基本特性和隔离级别
mysql econnreset_Nodejs 套接字报错处理 Error: read ECONNRESET
阿里云SLB负载均衡产品基本概念与购买流程
初次使用腾讯云,解决只能使用webshell连接,不能使用ssh连接。