当前位置:网站首页>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;
}
边栏推荐
- 学内核之三:使用GDB跟踪内核调用链
- 自主工业软件的创新与发展
- Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
- sql优化之explain
- 【Matlab】conv、filter、conv2、filter2和imfilter卷积函数总结
- Assertion of unittest framework
- LifeCycle
- Blob, text geometry or JSON column'xxx'can't have a default value query question
- R language uses dplyr package group_ The by function and the summarize function calculate the mean and standard deviation of the target variables based on the grouped variables
- Innovation and development of independent industrial software
猜你喜欢
随机推荐
R语言ggplot2可视化:gganimate包创建动画图(gif)、使用anim_save函数保存gif可视化动图
mac redis安装与使用,连接远程服务器 redis
Ruichengxin micro sprint technology innovation board: annual revenue of 367million, proposed to raise 1.3 billion, Datang Telecom is a shareholder
吃透Chisel语言.05.Chisel基础(二)——组合电路与运算符
[antd] how to set antd in form There is input in item Get input when gourp Value of each input of gourp
R language uses the mutation function of dplyr package to standardize the specified data column (using mean function and SD function), and calculates the grouping mean of the standardized target varia
吃透Chisel语言.12.Chisel项目构建、运行和测试(四)——Chisel测试之ChiselTest
2022游戏出海实用发行策略
R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
国内酒店交易DDD应用与实践——代码篇
Gorm read / write separation (rotation)
markdown 语法之字体标红
R language uses follow up of epidisplay package The plot function visualizes the longitudinal follow-up map of multiple ID (case) monitoring indicators, and uses stress The col parameter specifies the
The font of markdown grammar is marked in red
Introducing testfixture into unittest framework
Unittest框架之断言
测试流程整理(3)
C # WPF realizes the real-time screen capture function of screen capture box
Code hoof collection of wonderful secret place
卷积神经网络经典论文集合(深度学习分类篇)