视频网站做视频节目赚钱吗,阿里巴巴网站是用什么技术做的,一般淘宝网站做几个月赚钱,php做网站都需要学什么软件编写一种方法#xff0c;对字符串数组进行排序#xff0c;将所有变位词组合在一起。变位词是指字母相同#xff0c;但排列不同的字符串。
注意#xff1a;本题相对原题稍作修改
示例:
输入: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”], 输出: [ [“ate”,…编写一种方法对字符串数组进行排序将所有变位词组合在一起。变位词是指字母相同但排列不同的字符串。
注意本题相对原题稍作修改
示例:
输入: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”], 输出: [ [“ate”,“eat”,“tea”], [“nat”,“tan”], [“bat”] ] 说明
所有输入均为小写字母。不考虑答案输出的顺序。 解题思路
因为变位词是指字母相同但排列不同的字符串。因此我们可以对字符串进行排序将字符串按字典序排列而变位词的字母是相同的因此排序以后的结果对于所有变位词应该是相同的。
代码
class Solution {public ListListString groupAnagrams(String[] strs) {MapString,ListString mapnew HashMap();for (String str : strs) {char[] cstr.toCharArray();Arrays.sort(c);String s new String(c);if(!map.containsKey(s)){map.put(s,new ArrayList());}map.get(s).add(str);}ListListString resnew ArrayList();for (Map.EntryString, ListString entry : map.entrySet()) {res.add(entry.getValue());}return res;}
}