当前位置:网站首页>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;
}
边栏推荐
- Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]
- R language uses bwplot function in lattice package to visualize box plot and par Settings parameter custom theme mode
- Gorm data insertion (transfer)
- MATLAB中tiledlayout函数使用
- 自主工业软件的创新与发展
- Detailed index of MySQL
- 92.(cesium篇)cesium楼栋分层
- Golang 使用 JSON unmarshal 数字到 interface{} 数字变成 float64 类型(转)
- Basic mode of service mesh
- sharding key type not supported
猜你喜欢
Understand chisel language thoroughly 11. Chisel project construction, operation and test (III) -- scalatest of chisel test
[R language data science]: cross validation and looking back
Why should Base64 encoding be used for image transmission
JVM memory layout detailed, illustrated, well written!
Product identification of intelligent retail cabinet based on paddlex
Introducing testfixture into unittest framework
Xcode 异常图片导致ipa包增大问题
去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
markdown 语法之字体标红
Ruichengxin micro sprint technology innovation board: annual revenue of 367million, proposed to raise 1.3 billion, Datang Telecom is a shareholder
随机推荐
ARouter的使用
Xcode 异常图片导致ipa包增大问题
Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
QT how to detect whether the mouse is on a control
[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions
GCC【6】- 编译的4个阶段
Excel快速合并多行数据
Understand chisel language thoroughly 09. Chisel project construction, operation and testing (I) -- build and run chisel project with SBT
[C question set] of VII
sql优化之explain
Assertion of unittest framework
吃透Chisel语言.09.Chisel项目构建、运行和测试(一)——用sbt构建Chisel项目并运行
Haobo medical sprint technology innovation board: annual revenue of 260million Yonggang and Shen Zhiqun are the actual controllers
R语言使用dplyr包的group_by函数和summarise函数基于分组变量计算目标变量的均值、标准差
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
基于PaddleX的智能零售柜商品识别
奇妙秘境 码蹄集
学内核之三:使用GDB跟踪内核调用链
LiveData
Product identification of intelligent retail cabinet based on paddlex