49.Submission Details java solutions

Given an array of strings, group anagrams together.

For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"]
Return:

[
  ["ate", "eat","tea"],
  ["nat","tan"],
  ["bat"]
]

Note: All inputs will be in lower-case.

题意就是对同构词进行分类,存到list之后再返回。

 1 public class Solution {
 2     public List<List<String>> groupAnagrams(String[] strs) {
 3         Map<String,ArrayList<String>> map = new HashMap<String,ArrayList<String>>();
 4         for(String str : strs){
 5             String tmp = sortStr(str);
 6             if(map.containsKey(tmp)){
 7                 map.get(tmp).add(str);
 8             }else{
 9                 ArrayList<String> list = new ArrayList<String>();
10                 list.add(str);
11                 map.put(tmp,list);
12             }
13         }
14         List<List<String>> ans = new ArrayList<List<String>>(map.values());
15         // for(List<String> a : ans){
16         //     Collections.sort(a);
17         // }
18         return ans;
19     }
20
21     public String sortStr(String str){
22         char[] tmp = str.toCharArray();
23         Arrays.sort(tmp);
24         return new String(tmp);
25     }
26 }

时间: 2024-08-24 17:04:35

49.Submission Details java solutions的相关文章

亲測,Eclipse报&amp;quot;An error has occurred,See error log for more details. java.lang.NullPointerException&amp;quot;

Eclipse报"An error has occurred,See error log for more details. java.lang.NullPointerException", 我先说说我是怎么报这个错的吧,我是想打开Android项目res目录中layout目录里面的.xml布局文件的时候,就会报这个错. 然后我到网上搜索了一下,一般都是那几个答案,有一些答案不行,被骂的狗血淋头,所以我都不敢尝试用网上的方法.仅仅试用了一种"在"開始"--

an error has occurred. see error log for more details. java.lang.nullpointerexception 问题解决

解决办法:修改项目工作空间. 修改工作空间,以前打开myEclipse时知道怎么改!现在只有找配置文件了! 步骤: MyEclipse 5.1.1 GA----->Eclipse----->configuration------>.settings---->org.eclipse.ui.ide.prefs文件 #Tue Jul 28 09:10:40 CST 2009 RECENT_WORKSPACES_PROTOCOL=2 MAX_RECENT_WORKSPACES=5 SHOW

47. Permutations II java solutions

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations: [ [1,1,2], [1,2,1], [2,1,1] ] 1 public class Solution { 2 List<List<Integer>> ans

亲测,Eclipse报&quot;An error has occurred,See error log for more details. java.lang.NullPointerException&quot;

Eclipse报"An error has occurred,See error log for more details. java.lang.NullPointerException", 我先说说我是怎么报这个错的吧,我是想打开Android项目res文件夹中layout文件夹里面的.xml布局文件的时候,就会报这个错. 然后我到网上搜索了一下,一般都是那几个答案,有一些答案不行,被骂的狗血淋头,所以我都不敢尝试用网上的方法.只试用了一种"在"开始"-

119. Pascal&#39;s Triangle II java solutions

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 1 public class Solution { 2 public List<Integer> getRow(int rowIndex) { 3

63. Unique Paths II java solutions

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

eclipse报An error has occurred,See error log for more details. java.lang.NullPointerException错误

eclipse报An error has occurred,See error log for more details. java.lang.NullPointerException错误,解决办法: 在"开始"-->"运行"---->"cmd"进入命令提示行后,再进入eclipse安装目录的eclipse文件夹,         然后输入eclipse -clean即可

304. Range Sum Query 2D - Immutable java solutions

Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, co

An error has occurred. See error log for more details. java.lang.NullPointerException

昨天晚上下班eclipse都还好好的,今天早上一上班,eclipse老是弹窗显示An error has occurred. See error log for more details. java.lang.NullPointerException 的错误提示.在网上一搜,说各种情况的都有,但是我看错误提示应该是日志有什么问题.我就把apache-tomcat-8.0.23的logs目录下文件全都删除,问题解决. 具体目录如下:E:\develop_tools\apache-tomcat-8.