当前位置:网站首页>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());
}
}
代码说明
注释见。。。
边栏推荐
- Shu tianmeng map × Weiyan technology - Dream map database circle of friends + 1
- The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched
- [deep learning paper notes] hnf-netv2 for segmentation of brain tumors using multimodal MR imaging
- Flutter InkWell & Ink组件
- MySQL splits strings for conditional queries
- 关于 Notion-Like 工具的反思和畅想
- Solve Unicode decodeerror: 'GBK' codec can't decode byte 0xa2 in position 107
- RHCSA8
- Concurrent performance test of SAP Spartacus with JMeter
- A specific example of ABAP type and EDM type mapping in SAP segw transaction code
猜你喜欢

OpenHarmony应用开发之Navigation组件详解

Reverse Polish notation
![[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel](/img/b6/f9da8a36167db10c9a92dabb166c81.png)
[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel

Jenkins installation

Alibaba cloud SLB load balancing product basic concept and purchase process
![[daily question] 1200 Minimum absolute difference](/img/2f/9214df63f6d5fafa1f7247c4529643.png)
[daily question] 1200 Minimum absolute difference

Android本地Sqlite数据库的备份和还原

精彩速递|腾讯云数据库6月刊

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

简单上手的页面请求和解析案例
随机推荐
Fragmented knowledge management tool memos
Flutter 3.0更新后如何应用到小程序开发中
【Hot100】33. 搜索旋转排序数组
Navigation property and entityset usage in SAP segw transaction code
[daily question] 1200 Minimum absolute difference
Put functions in modules
MySQL - database query - sort query, paging query
函数的默认参数&函数参数的多种方法
什么是网络端口
FPGA learning notes: vivado 2019.1 add IP MicroBlaze
Although the volume and price fall, why are the structural deposits of commercial banks favored by listed companies?
Can and can FD
记录一下在深度学习-一些bug处理
今年上半年,通信行业发生了哪些事?
Word document injection (tracking word documents) incomplete
多人合作项目查看每个人写了多少行代码
一文详解ASCII码,Unicode与utf-8
Notion 类笔记软件如何选择?Notion 、FlowUs 、Wolai 对比评测
MySQL splits strings for conditional queries
Summary and arrangement of JPA specifications