当前位置:网站首页>力扣.字母异位词分组
力扣.字母异位词分组
2022-07-31 05:17:00 【旺仔 小馒头】
给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。
字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母通常恰好只用一次。
示例 1:
输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
输出: [["bat"],["nat","tan"],["ate","eat","tea"]]
示例 2:
输入: strs = [""]
输出: [[""]]
示例 3:
输入: strs = ["a"]
输出: [["a"]]
思路
首先遇到异位词,一定要想到用hash表来解决,首先字符串是无序的,我们可以通过STL中的sort方法将字符串排序,这样所有的异位词都会变成同一种排列顺序的串,于是遍历这个字符串数组,将对应的串当作hash表的键值key,将排序以前的串当作key对应的value,因为返回的仍然是一条条串组成的数组,所以map对应的value类型不能是普通的string,而是一个vector<string>, vector容器中的元素是各种字符串。
(意思就是:建立一个unordered_map<string ,vector<string> >一个字符串 ,str排序后当作key,排序前当value中的一个个子集)。
遍历strs,遇到相同key的串就添加进容器。
代码如下:
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
// 用于接收最后的结果
vector<vector<string>> ret;
// 建立一个hash_map
unordered_map<string,vector<string>> m;
for (auto& str : strs){
// 这里一定需要一个中间变量,如果直接用m[str].push_back(str),
// str会在排序后错乱,最终插入的str顺序不再是之前的顺序而是排过序后的
string key = str;
// 将字符串进行排序,这样异位词排序后,都是一样的key
sort(key.begin(),key.end());
// 用相同的key当作键值,并将其对应的原字符串当作value,压入value的vector容器
m[key].push_back(str);
}
// 最终遍历m,将相同key对应的value(一个vector<string>容器:存着各种异位词串),分别压入ret
// 容器中
for (auto e : m)
ret.push_back(e.second);
return ret;
}
};
边栏推荐
- ROS service transfer pictures
- 多线程截取视频为每帧
- a:自我介绍
- Software Testing Interview Questions 2021
- Attention based ASR(LAS)
- Pytorch study notes 7 - processing input of multi-dimensional features
- Cholesterol-PEG-Thiol CLS-PEG-SH Cholesterol-Polyethylene Glycol-Sulfhydryl
- The content of the wangeditor editor is transferred to the background server for storage
- [Solved] ssh connection report: Bad owner or permissions on C:\\Users/XXX/.ssh/config
- 日志jar包冲突,及其解决方法
猜你喜欢

Log jar package conflict, and its solution

DSPE-PEG-Thiol DSPE-PEG-SH 磷脂-聚乙二醇-巯基脂质体制备用

Pytorch study notes 10 - detailed explanation of convolutional neural network and application of multi-classification task of mnist dataset

Phospholipids-Polyethylene Glycol-Active Esters for Scientific Research DSPE-PEG-NHS CAS: 1445723-73-8

四种常见的POST提交数据方式

计算图像数据集均值和方差

Detailed explanation of mysql transaction principle

Embedding cutting-edge understanding

2022年SQL大厂高频实战面试题(详细解析)

深度学习知识点杂谈
随机推荐
概率论相关笔记
Cholesterol-PEG-Azide CLS-PEG-N3 Cholesterol-PEG-Azide MW:3400
安装显卡过程中遇到问题汇总
crontab timing operation
ImportError: cannot import name ‘Xxxx‘ from partially initialized module ‘xx.xx.xx‘
2022 SQL big factory high-frequency practical interview questions (detailed analysis)
Tensorflow边用边踩坑
ROS之service传输图片
C语言结构体(必须掌握版)
ImportError: cannot import name 'Xxxx' from partially initialized module 'xx.xx.xx'
Chemical Reagent Phospholipid-Polyethylene Glycol-Amino, DSPE-PEG-amine, CAS: 474922-26-4
[已解决]ssh连接报:Bad owner or permissions on C:\\Users/XXX/.ssh/config
2021-09-30
2021年软件测试面试题大全
会话和饼干,令牌
Cholesterol-PEG-Acid CLS-PEG-COOH 胆固醇-聚乙二醇-羧基修饰肽类化合物
Fluorescein-PEG-DSPE 磷脂-聚乙二醇-荧光素荧光磷脂PEG衍生物
mPEG-DSPE 178744-28-0 甲氧基-聚乙二醇-磷脂酰乙醇胺线性PEG磷脂
pytorch学习笔记10——卷积神经网络详解及mnist数据集多分类任务应用
IDEA控制台不能输入信息的解决方法