Skip to content

Commit 349df81

Browse files
committed
49. group anagramscopy for markdowncopy for markdown
1 parent 94eecf8 commit 349df81

6 files changed

Lines changed: 178 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*.tar.gz
2222
*.rar
2323
out/
24+
target/
2425
.idea/
2526
.DS_Store
2627

LeetCode-Java.iml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<excludeFolder url="file://$MODULE_DIR$/target" />
9+
</content>
10+
<orderEntry type="inheritedJdk" />
11+
<orderEntry type="sourceFolder" forTests="false" />
12+
</component>
13+
</module>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
### [49\. group anagramscopy for markdowncopy for markdown](https://leetcode.com/problems/group-anagrams/)
2+
3+
difficulty: **medium**
4+
5+
6+
given an array of strings, group anagrams together.
7+
8+
**example:**
9+
10+
```
11+
input: ["eat", "tea", "tan", "ate", "nat", "bat"],
12+
output:
13+
[
14+
["ate","eat","tea"],
15+
["nat","tan"],
16+
["bat"]
17+
]```
18+
19+
**note:**
20+
21+
* all inputs will be in lowercase.
22+
* the order of your output does not matter.
23+
24+
25+
#### solution
26+
27+
language: **java**
28+
29+
```java
30+
class solution {
31+
   public list<list<string>> groupanagrams(string[] strs) {
32+
       list<list<string>> resultlist = new arraylist<>();
33+
       if (strs == null || strs.length == 0) {
34+
           return resultlist;
35+
      }
36+
       map<string,integer> groupsmap = new hashmap<>();
37+
       // 这里的 key 需要是 string,而不能使用数组,如果使用数组的话,key是内存地址,无法满足要求。
38+
       // value 存储的是这个组的字符串的 index
39+
       for (int i = 0; i < strs.length; i++) {
40+
           string str = strs[i];
41+
           char[] chars = str.tochararray();
42+
           arrays.sort(chars);
43+
           string s = string.valueof(chars); // 排序后得到最新的字符串
44+
           if (groupsmap.containskey(s)) {
45+
               resultlist.get(groupsmap.get(s)).add(str);
46+
               continue;
47+
          }
48+
           groupsmap.put(s,resultlist.size());
49+
           list<string> resultitem = new arraylist<>();
50+
           resultitem.add(str);
51+
           resultlist.add(resultitem);
52+
      }
53+
       return resultlist;
54+
  }
55+
}
56+
```
57+
![](http://ww4.sinaimg.cn/large/006tNc79ly1g4tvny2r5uj31b60qiq7l.jpg)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode._49_;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Created by zhangbo54 on 2019-03-04.
7+
*/
8+
public class Main {
9+
public static void main(String[] args) {
10+
Solution solution = new Solution();
11+
String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
12+
List<List<String>> lists = solution.groupAnagrams(strs);
13+
System.out.println(lists);
14+
}
15+
}
16+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package leetcode._49_;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
class Solution {
10+
public List<List<String>> groupAnagrams(String[] strs) {
11+
List<List<String>> resultList = new ArrayList<>();
12+
if (strs == null || strs.length == 0) {
13+
return resultList;
14+
}
15+
Map<String,Integer> groupsMap = new HashMap<>();
16+
// 这里的 key 需要是 String,而不能使用数组,如果使用数组的话,key是内存地址,无法满足要求。
17+
// value 存储的是这个组的字符串的 index
18+
for (int i = 0; i < strs.length; i++) {
19+
String str = strs[i];
20+
char[] chars = str.toCharArray();
21+
Arrays.sort(chars);
22+
String s = String.valueOf(chars); // 排序后得到最新的字符串
23+
if (groupsMap.containsKey(s)) {
24+
resultList.get(groupsMap.get(s)).add(str);
25+
continue;
26+
}
27+
groupsMap.put(s,resultList.size());
28+
List<String> resultItem = new ArrayList<>();
29+
resultItem.add(str);
30+
resultList.add(resultItem);
31+
}
32+
return resultList;
33+
}
34+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
### [49\. group anagramscopy for markdowncopy for markdown](https://leetcode.com/problems/group-anagrams/)
2+
3+
difficulty: **medium**
4+
5+
6+
given an array of strings, group anagrams together.
7+
8+
**example:**
9+
10+
```
11+
input: ["eat", "tea", "tan", "ate", "nat", "bat"],
12+
output:
13+
[
14+
["ate","eat","tea"],
15+
["nat","tan"],
16+
["bat"]
17+
]```
18+
19+
**note:**
20+
21+
* all inputs will be in lowercase.
22+
* the order of your output does not matter.
23+
24+
25+
#### solution
26+
27+
language: **java**
28+
29+
```java
30+
class solution {
31+
   public list<list<string>> groupanagrams(string[] strs) {
32+
       list<list<string>> resultlist = new arraylist<>();
33+
       if (strs == null || strs.length == 0) {
34+
           return resultlist;
35+
      }
36+
       map<string,integer> groupsmap = new hashmap<>();
37+
       // 这里的 key 需要是 string,而不能使用数组,如果使用数组的话,key是内存地址,无法满足要求。
38+
       // value 存储的是这个组的字符串的 index
39+
       for (int i = 0; i < strs.length; i++) {
40+
           string str = strs[i];
41+
           char[] chars = str.tochararray();
42+
           arrays.sort(chars);
43+
           string s = string.valueof(chars); // 排序后得到最新的字符串
44+
           if (groupsmap.containskey(s)) {
45+
               resultlist.get(groupsmap.get(s)).add(str);
46+
               continue;
47+
          }
48+
           groupsmap.put(s,resultlist.size());
49+
           list<string> resultitem = new arraylist<>();
50+
           resultitem.add(str);
51+
           resultlist.add(resultitem);
52+
      }
53+
       return resultlist;
54+
  }
55+
}
56+
```
57+
![](http://ww4.sinaimg.cn/large/006tNc79ly1g4tvny2r5uj31b60qiq7l.jpg)

0 commit comments

Comments
 (0)