当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
Test evaluation of software testing
吃透Chisel语言.09.Chisel项目构建、运行和测试(一)——用sbt构建Chisel项目并运行
按照功能对Boost库进行分类
Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]
Rich text editing: wangeditor tutorial
MySQL version 8 installation Free Tutorial
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
Unity shader learning (3) try to draw a circle
Apple 5g chip research and development failure: continue to rely on Qualcomm, but also worry about being prosecuted?
测试流程整理(3)
随机推荐
[antd step pit] antd form cooperates with input Form The height occupied by item is incorrect
What is the real meaning and purpose of doing things, and what do you really want
R语言dplyr包summarise_if函数计算dataframe数据中所有数值数据列的均值和中位数、基于条件进行数据汇总分析(Summarize all Numeric Variables)
LifeCycle
Why should Base64 encoding be used for image transmission
做事的真正意义和目的,真正想得到什么
Whether the loyalty agreement has legal effect
Xcode 异常图片导致ipa包增大问题
R language uses bwplot function in lattice package to visualize box plot and par Settings parameter custom theme mode
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
sql优化之explain
Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
Data warehouse interview question preparation
Code hoof collection of wonderful secret place
How to package QT and share exe
China Post technology rushes to the scientific innovation board: the annual revenue is 2.058 billion, and the postal group is the major shareholder
测试流程整理(3)
基于51单片机的超声波测距仪
Error in find command: paths must precede expression (turn)
R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据