当前位置:网站首页>Leetcode T49: 字母异位词分组
Leetcode T49: 字母异位词分组
2022-07-04 12:51:00 【范谦之】
题目描述
给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。
字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母通常恰好只用一次。
示例 1:
输入: strs = [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”]
输出: [[“bat”],[“nat”,“tan”],[“ate”,“eat”,“tea”]]
示例 2:
输入: strs = [“”]
输出: [[“”]]
示例 3:
输入: strs = [“a”]
输出: [[“a”]]
提示:
- 1 <= strs.length <= 104
- 0 <= strs[i].length <= 100
- strs[i] 仅包含小写字母
思路
如果两个字符串再根据字符进行排序后,得到的字符串相等,那么这两个字符串为字母异位词。
使用HashMap,提高存取效率。
代码
Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
public List<List<String>> groupAnagrams(String[] strs) {
for(int i = 0; i < strs.length; i++) {
char[] chs = strs[i].toCharArray();
Arrays.sort(chs);
if(map.containsKey(new String(chs))) {
map.get(new String(chs)).add(i);
} else {
List<Integer> lis = new ArrayList<Integer>();
lis.add(i);
map.put(new String(chs), lis);
}
}
List<List<String>> res = new ArrayList<List<String>>();
for(List<Integer> lis: map.values()) {
List<String> tmp = new ArrayList<String>();
for(int ind: lis) tmp.add(strs[ind]);
res.add(tmp);
}
return res;
}
边栏推荐
- Fs4059c is a 5V input boost charging 12.6v1.2a. Inputting a small current to three lithium battery charging chips will not pull it dead. The temperature is 60 ° and 1000-1100ma is recommended
- 如何游戏出海代运营、游戏出海代投
- R语言ggplot2可视化:gganimate包创建动画图(gif)、使用anim_save函数保存gif可视化动图
- Unity shader learning (3) try to draw a circle
- go语言中的文件创建,写入,读取,删除(转)
- Huahao Zhongtian rushes to the scientific and Technological Innovation Board: the annual loss is 280million, and it is proposed to raise 1.5 billion. Beida pharmaceutical is a shareholder
- BLOB,TEXT GEOMETRY or JSON column 'xxx' can't have a default value query 问题
- 吃透Chisel语言.10.Chisel项目构建、运行和测试(二)——Chisel中生成Verilog代码&Chisel开发流程
- How to package QT and share exe
- Unittest框架中引入TestFixture
猜你喜欢

吃透Chisel语言.05.Chisel基础(二)——组合电路与运算符

自主工业软件的创新与发展

China Post technology rushes to the scientific innovation board: the annual revenue is 2.058 billion, and the postal group is the major shareholder

国内酒店交易DDD应用与实践——代码篇

Innovation and development of independent industrial software

sharding key type not supported

Understand chisel language thoroughly 06. Chisel Foundation (III) -- registers and counters

MATLAB中tiledlayout函数使用
![[antd step pit] antd form cooperates with input Form The height occupied by item is incorrect](/img/96/379d1692f9d3c05a7af2e938cbc5d7.png)
[antd step pit] antd form cooperates with input Form The height occupied by item is incorrect

为什么图片传输要使用base64编码
随机推荐
【信息检索】链接分析
Understand chisel language thoroughly 08. Chisel Foundation (V) -- wire, REG and IO, and how to understand chisel generation hardware
ViewModel 初体验
The game goes to sea and operates globally
R语言使用dplyr包的mutate函数对指定数据列进行标准化处理(使用mean函数和sd函数)并基于分组变量计算标准化后的目标变量的分组均值
Excel quickly merges multiple rows of data
R language uses bwplot function in lattice package to visualize box plot and par Settings parameter custom theme mode
Migration from go vendor project to mod project
Understand chisel language thoroughly 12. Chisel project construction, operation and testing (IV) -- chisel test of chisel test
吃透Chisel语言.04.Chisel基础(一)——信号类型和常量
奇妙秘境 码蹄集
Deming Lee listed on Shenzhen Stock Exchange: the market value is 3.1 billion, which is the husband and wife of Li Hu and Tian Hua
【C 题集】of Ⅶ
MATLAB中tiledlayout函数使用
Unity shader learning (3) try to draw a circle
BLOB,TEXT GEOMETRY or JSON column 'xxx' can't have a default value query 问题
MySQL之详解索引
Basic mode of service mesh
Gorm read / write separation (rotation)
吃透Chisel语言.08.Chisel基础(五)——Wire、Reg和IO,以及如何理解Chisel生成硬件