Skip to content

Commit 31f7db7

Browse files
committed
49. group anagramscopy for markdowncopy for markdown
1 parent 8baa038 commit 31f7db7

File tree

3 files changed

+53
-53
lines changed

3 files changed

+53
-53
lines changed

markdown/49. group anagrams.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,31 @@ output:
2828
language: **java**
2929

3030
```java
31-
class solution {
32-
   public list<list<string>> groupanagrams(string[] strs) {
33-
       list<list<string>> resultlist = new arraylist<>();
34-
       if (strs == null || strs.length == 0) {
35-
           return resultlist;
36-
      }
37-
       map<string,integer> groupsmap = new hashmap<>();
38-
       // 这里的 key 需要是 string,而不能使用数组,如果使用数组的话,key是内存地址,无法满足要求。
39-
       // value 存储的是这个组的字符串的 index
40-
       for (int i = 0; i < strs.length; i++) {
41-
           string str = strs[i];
42-
           char[] chars = str.tochararray();
43-
           arrays.sort(chars);
44-
           string s = string.valueof(chars); // 排序后得到最新的字符串
45-
           if (groupsmap.containskey(s)) {
46-
               resultlist.get(groupsmap.get(s)).add(str);
47-
               continue;
48-
          }
49-
           groupsmap.put(s,resultlist.size());
50-
           list<string> resultitem = new arraylist<>();
51-
           resultitem.add(str);
52-
           resultlist.add(resultitem);
53-
      }
54-
       return resultlist;
55-
  }
31+
class Solution {
32+
public List<List<String>> groupAnagrams(String[] strs) {
33+
List<List<String>> resultList = new ArrayList<>();
34+
if (strs == null || strs.length == 0) {
35+
return resultList;
36+
}
37+
Map<String,List<String>> groupsMap = new HashMap<>();
38+
// 这里的 key 需要是 String,而不能使用数组,如果使用数组的话,key是内存地址,无法满足要求。
39+
// value 存储的是这个组的字符串的 index
40+
for (int i = 0; i < strs.length; i++) {
41+
String str = strs[i];
42+
char[] chars = str.toCharArray();
43+
Arrays.sort(chars);
44+
String s = String.valueOf(chars); // 排序后得到最新的字符串
45+
if (groupsMap.containsKey(s)) {
46+
groupsMap.get(s).add(str);
47+
continue;
48+
}
49+
List<String> resultItem = new ArrayList<>();
50+
groupsMap.put(s,resultItem);
51+
resultItem.add(str);
52+
resultList.add(resultItem);
53+
}
54+
return resultList;
55+
}
5656
}
5757
```
5858
![](http://ww4.sinaimg.cn/large/006tNc79ly1g4tvny2r5uj31b60qiq7l.jpg)

src/main/java/leetcode/_49_/Solution.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public List<List<String>> groupAnagrams(String[] strs) {
1212
if (strs == null || strs.length == 0) {
1313
return resultList;
1414
}
15-
Map<String,Integer> groupsMap = new HashMap<>();
15+
Map<String,List<String>> groupsMap = new HashMap<>();
1616
// 这里的 key 需要是 String,而不能使用数组,如果使用数组的话,key是内存地址,无法满足要求。
1717
// value 存储的是这个组的字符串的 index
1818
for (int i = 0; i < strs.length; i++) {
@@ -21,11 +21,11 @@ public List<List<String>> groupAnagrams(String[] strs) {
2121
Arrays.sort(chars);
2222
String s = String.valueOf(chars); // 排序后得到最新的字符串
2323
if (groupsMap.containsKey(s)) {
24-
resultList.get(groupsMap.get(s)).add(str);
24+
groupsMap.get(s).add(str);
2525
continue;
2626
}
27-
groupsMap.put(s,resultList.size());
2827
List<String> resultItem = new ArrayList<>();
28+
groupsMap.put(s,resultItem);
2929
resultItem.add(str);
3030
resultList.add(resultItem);
3131
}

src/main/java/leetcode/_49_/solution.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,31 @@ output:
2828
language: **java**
2929

3030
```java
31-
class solution {
32-
   public list<list<string>> groupanagrams(string[] strs) {
33-
       list<list<string>> resultlist = new arraylist<>();
34-
       if (strs == null || strs.length == 0) {
35-
           return resultlist;
36-
      }
37-
       map<string,integer> groupsmap = new hashmap<>();
38-
       // 这里的 key 需要是 string,而不能使用数组,如果使用数组的话,key是内存地址,无法满足要求。
39-
       // value 存储的是这个组的字符串的 index
40-
       for (int i = 0; i < strs.length; i++) {
41-
           string str = strs[i];
42-
           char[] chars = str.tochararray();
43-
           arrays.sort(chars);
44-
           string s = string.valueof(chars); // 排序后得到最新的字符串
45-
           if (groupsmap.containskey(s)) {
46-
               resultlist.get(groupsmap.get(s)).add(str);
47-
               continue;
48-
          }
49-
           groupsmap.put(s,resultlist.size());
50-
           list<string> resultitem = new arraylist<>();
51-
           resultitem.add(str);
52-
           resultlist.add(resultitem);
53-
      }
54-
       return resultlist;
55-
  }
31+
class Solution {
32+
public List<List<String>> groupAnagrams(String[] strs) {
33+
List<List<String>> resultList = new ArrayList<>();
34+
if (strs == null || strs.length == 0) {
35+
return resultList;
36+
}
37+
Map<String,List<String>> groupsMap = new HashMap<>();
38+
// 这里的 key 需要是 String,而不能使用数组,如果使用数组的话,key是内存地址,无法满足要求。
39+
// value 存储的是这个组的字符串的 index
40+
for (int i = 0; i < strs.length; i++) {
41+
String str = strs[i];
42+
char[] chars = str.toCharArray();
43+
Arrays.sort(chars);
44+
String s = String.valueOf(chars); // 排序后得到最新的字符串
45+
if (groupsMap.containsKey(s)) {
46+
groupsMap.get(s).add(str);
47+
continue;
48+
}
49+
List<String> resultItem = new ArrayList<>();
50+
groupsMap.put(s,resultItem);
51+
resultItem.add(str);
52+
resultList.add(resultItem);
53+
}
54+
return resultList;
55+
}
5656
}
5757
```
5858
![](http://ww4.sinaimg.cn/large/006tNc79ly1g4tvny2r5uj31b60qiq7l.jpg)

0 commit comments

Comments
 (0)