Skip to content

Commit d6a324c

Browse files
committed
commit solution 49 187 204
1 parent 6d2c002 commit d6a324c

File tree

14 files changed

+298
-12
lines changed

14 files changed

+298
-12
lines changed

Diff for: index-tags.md

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
| [13](https://leetcode-cn.com/problems/roman-to-integer) | [罗马数字转整数](/solution/1-99/0013.roman-to-integer/) | `数学`,`字符串` | <font color=green>简单</font> ||
3333
| [20](https://leetcode-cn.com/problems/valid-parentheses) | [有效的括号](/solution/1-99/0020.valid-parentheses/) | ``,`字符串` | <font color=green>简单</font> ||
3434
| [28](https://leetcode-cn.com/problems/implement-strstr) | [实现 strstr()](/solution/1-99/0028.implement-strstr%28%29/) | `双指针`,`字符串` | <font color=green>简单</font> ||
35+
| [49](https://leetcode-cn.com/problems/group-anagrams) | [字母异位词分组](/solution/1-99/0049.group-anagrams/) | `哈希表`,`字符串` | <font color=blue>中等</font> ||
3536
| [58](https://leetcode-cn.com/problems/length-of-last-word) | [最后一个单词的长度](/solution/1-99/0058.length-of-last-word/) | `字符串` | <font color=green>简单</font> ||
3637
| [125](https://leetcode-cn.com/problems/valid-palindrome) | [验证回文串](/solution/100-199/0125.valid-palindrome/) | `双指针`,`字符串` | <font color=green>简单</font> ||
3738
| [344](https://leetcode-cn.com/problems/reverse-string) | [反转字符串](/solution/300-399/0344.reverse-string/) | `双指针`,`字符串` | <font color=green>简单</font> ||
@@ -46,6 +47,7 @@
4647
| [9](https://leetcode-cn.com/problems/palindrome-number) | [回文数](/solution/1-99/0009.palindrome-number/) | `数学` | <font color=green>简单</font> ||
4748
| [13](https://leetcode-cn.com/problems/roman-to-integer) | [罗马数字转整数](/solution/1-99/0013.roman-to-integer/) | `数学`,`字符串` | <font color=green>简单</font> ||
4849
| [202](https://leetcode-cn.com/problems/happy-number) | [快乐数](/solution/200-299/0202.happy-number/) | `哈希表`,`数学` | <font color=green>简单</font> ||
50+
| [204](https://leetcode-cn.com/problems/count-primes) | [计数质数](/solution/200-299/0204.count-primes/) | `哈希表`,`数学` | <font color=green>简单</font> ||
4951
| [268](https://leetcode-cn.com/problems/missing-number) | [缺失数字](/solution/200-299/0268.missing-number/) | `位运算`,`数组`,`数学` | <font color=green>简单</font> ||
5052

5153
#### **哈希表**
@@ -54,8 +56,11 @@
5456
| --- | --- | --- | --- | --- |
5557
| [1](https://leetcode-cn.com/problems/two-sum) | [两数之和](/solution/1-99/0001.two-sum/) | `数组`,`哈希表` | <font color=green>简单</font> ||
5658
| [36](https://leetcode-cn.com/problems/valid-sudoku) | [有效的数独](/solution/1-99/0036.valid-sudoku/) | `哈希表` | <font color=blue>中等</font> ||
59+
| [49](https://leetcode-cn.com/problems/group-anagrams) | [字母异位词分组](/solution/1-99/0049.group-anagrams/) | `哈希表`,`字符串` | <font color=blue>中等</font> ||
5760
| [136](https://leetcode-cn.com/problems/single-number) | [只出现一次的数字](/solution/100-199/0136.single-number/) | `位运算`,`哈希表` | <font color=green>简单</font> ||
61+
| [187](https://leetcode-cn.com/problems/repeated-dna-sequences) | [重复的dna序列](/solution/100-199/0187.repeated-dna-sequences/) | `位运算`,`哈希表` | <font color=blue>中等</font> ||
5862
| [202](https://leetcode-cn.com/problems/happy-number) | [快乐数](/solution/200-299/0202.happy-number/) | `哈希表`,`数学` | <font color=green>简单</font> ||
63+
| [204](https://leetcode-cn.com/problems/count-primes) | [计数质数](/solution/200-299/0204.count-primes/) | `哈希表`,`数学` | <font color=green>简单</font> ||
5964
| [217](https://leetcode-cn.com/problems/contains-duplicate) | [存在重复元素](/solution/200-299/0217.contains-duplicate/) | `数组`,`哈希表` | <font color=green>简单</font> ||
6065
| [219](https://leetcode-cn.com/problems/contains-duplicate-ii) | [存在重复元素 ii](/solution/200-299/0219.contains-duplicate-ii/) | `数组`,`哈希表` | <font color=green>简单</font> ||
6166

@@ -109,6 +114,7 @@
109114
| --- | --- | --- | --- | --- |
110115
| [136](https://leetcode-cn.com/problems/single-number) | [只出现一次的数字](/solution/100-199/0136.single-number/) | `位运算`,`哈希表` | <font color=green>简单</font> ||
111116
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
117+
| [187](https://leetcode-cn.com/problems/repeated-dna-sequences) | [重复的dna序列](/solution/100-199/0187.repeated-dna-sequences/) | `位运算`,`哈希表` | <font color=blue>中等</font> ||
112118
| [268](https://leetcode-cn.com/problems/missing-number) | [缺失数字](/solution/200-299/0268.missing-number/) | `位运算`,`数组`,`数学` | <font color=green>简单</font> ||
113119

114120
#### ****

Diff for: index-type.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
| [198](https://leetcode-cn.com/problems/house-robber) | [打家劫舍](/solution/100-199/0198.house-robber/) | `动态规划` | <font color=green>简单</font> |
6767
| [202](https://leetcode-cn.com/problems/happy-number) | [快乐数](/solution/200-299/0202.happy-number/) | `哈希表`,`数学` | <font color=green>简单</font> ||
6868
| [203](https://leetcode-cn.com/problems/remove-linked-list-elements) | [移除链表元素](/solution/200-299/0203.remove-linked-list-elements/) | `链表` | <font color=green>简单</font> |
69-
| [204](https://leetcode-cn.com/problems/count-primes) | [计数质数](/solution/200-299/0204.count-primes/) | `哈希表`,`数学` | <font color=green>简单</font> |
69+
| [204](https://leetcode-cn.com/problems/count-primes) | [计数质数](/solution/200-299/0204.count-primes/) | `哈希表`,`数学` | <font color=green>简单</font> ||
7070
| [205](https://leetcode-cn.com/problems/isomorphic-strings) | [同构字符串](/solution/200-299/0205.isomorphic-strings/) | `哈希表` | <font color=green>简单</font> |
7171
| [206](https://leetcode-cn.com/problems/reverse-linked-list) | [反转链表](/solution/200-299/0206.reverse-linked-list/) | `链表` | <font color=green>简单</font> |
7272
| [217](https://leetcode-cn.com/problems/contains-duplicate) | [存在重复元素](/solution/200-299/0217.contains-duplicate/) | `数组`,`哈希表` | <font color=green>简单</font> ||
@@ -430,7 +430,7 @@
430430
| [46](https://leetcode-cn.com/problems/permutations) | [全排列](/solution/1-99/0046.permutations/) | `回溯算法` | <font color=blue>中等</font> |
431431
| [47](https://leetcode-cn.com/problems/permutations-ii) | [全排列 ii](/solution/1-99/0047.permutations-ii/) | `回溯算法` | <font color=blue>中等</font> |
432432
| [48](https://leetcode-cn.com/problems/rotate-image) | [旋转图像](/solution/1-99/0048.rotate-image/) | `数组` | <font color=blue>中等</font> |
433-
| [49](https://leetcode-cn.com/problems/group-anagrams) | [字母异位词分组](/solution/1-99/0049.group-anagrams/) | `哈希表`,`字符串` | <font color=blue>中等</font> |
433+
| [49](https://leetcode-cn.com/problems/group-anagrams) | [字母异位词分组](/solution/1-99/0049.group-anagrams/) | `哈希表`,`字符串` | <font color=blue>中等</font> ||
434434
| [50](https://leetcode-cn.com/problems/powx-n) | [pow(x, n)](/solution/1-99/0050.pow%28x%2c-n%29/) | `数学`,`二分查找` | <font color=blue>中等</font> |
435435
| [54](https://leetcode-cn.com/problems/spiral-matrix) | [螺旋矩阵](/solution/1-99/0054.spiral-matrix/) | `数组` | <font color=blue>中等</font> |
436436
| [55](https://leetcode-cn.com/problems/jump-game) | [跳跃游戏](/solution/1-99/0055.jump-game/) | `贪心算法`,`数组` | <font color=blue>中等</font> |
@@ -504,7 +504,7 @@
504504
| [180](https://leetcode-cn.com/problems/consecutive-numbers) | [连续出现的数字](/solution/100-199/0180.consecutive-numbers/) | | <font color=blue>中等</font> |
505505
| [184](https://leetcode-cn.com/problems/department-highest-salary) | [部门工资最高的员工](/solution/100-199/0184.department-highest-salary/) | | <font color=blue>中等</font> |
506506
| [186](https://leetcode-cn.com/problems/reverse-words-in-a-string-ii) | [翻转字符串里的单词 ii](/solution/100-199/0186.reverse-words-in-a-string-ii/) | `字符串` | <font color=blue>中等</font> |
507-
| [187](https://leetcode-cn.com/problems/repeated-dna-sequences) | [重复的dna序列](/solution/100-199/0187.repeated-dna-sequences/) | `位运算`,`哈希表` | <font color=blue>中等</font> |
507+
| [187](https://leetcode-cn.com/problems/repeated-dna-sequences) | [重复的dna序列](/solution/100-199/0187.repeated-dna-sequences/) | `位运算`,`哈希表` | <font color=blue>中等</font> ||
508508
| [192](https://leetcode-cn.com/problems/word-frequency) | [统计词频](/solution/100-199/0192.word-frequency/) | | <font color=blue>中等</font> |
509509
| [194](https://leetcode-cn.com/problems/transpose-file) | [转置文件](/solution/100-199/0194.transpose-file/) | | <font color=blue>中等</font> |
510510
| [199](https://leetcode-cn.com/problems/binary-tree-right-side-view) | [二叉树的右视图](/solution/100-199/0199.binary-tree-right-side-view/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=blue>中等</font> |

Diff for: solution/1-99/0049.group-anagrams/README.md

+30-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# [49. 字母异位词分组](https://leetcode-cn.com/problems/group-anagrams)
22

33
### 题目描述
4-
<!-- 这里写题目描述 -->
4+
55
<p>给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。</p>
66

77
<p><strong>示例:</strong></p>
@@ -25,15 +25,39 @@
2525

2626
### 解题思路
2727

28+
1. 对字符串排序
29+
2. 利用hash table判断是否存在,如果已存在,则将该字符串分配到已存在的返回数组中
2830

2931
### 具体解法
3032

31-
<!-- tabs:start -->
32-
3333
#### **Golang**
3434
```go
35-
35+
func groupAnagrams(strs []string) [][]string {
36+
strMap := make(map[string]int)
37+
var res [][]string
38+
mapIndex := 0
39+
for _, str := range strs {
40+
strSort := sortString(str)
41+
42+
if _, ok := strMap[strSort]; !ok {
43+
res = append(res, []string{str})
44+
strMap[strSort] = mapIndex
45+
mapIndex++
46+
} else {
47+
res[strMap[strSort]] = append(res[strMap[strSort]], str)
48+
}
49+
}
50+
return res
51+
}
52+
53+
func sortString(str string) string {
54+
strR := []rune(str)
55+
strArr := []string{}
56+
for i := 0; i < len(strR); i++ {
57+
strArr = append(strArr, string(strR[i]))
58+
}
59+
sort.Strings(strArr)
60+
return strings.Join(strArr, "")
61+
}
3662
```
3763

38-
<!-- tabs:end -->
39-

Diff for: solution/1-99/0049.group-anagrams/solution.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package leetcode
2+
3+
import (
4+
"sort"
5+
"strings"
6+
)
7+
8+
/*
9+
* @lc app=leetcode.cn id=49 lang=golang
10+
*
11+
* [49] 字母异位词分组
12+
*/
13+
14+
// @lc code=start
15+
func groupAnagrams(strs []string) [][]string {
16+
strMap := make(map[string]int)
17+
var res [][]string
18+
mapIndex := 0
19+
for _, str := range strs {
20+
strSort := sortString(str)
21+
22+
if _, ok := strMap[strSort]; !ok {
23+
res = append(res, []string{str})
24+
strMap[strSort] = mapIndex
25+
mapIndex++
26+
} else {
27+
res[strMap[strSort]] = append(res[strMap[strSort]], str)
28+
}
29+
}
30+
return res
31+
}
32+
33+
func sortString(str string) string {
34+
strR := []rune(str)
35+
strArr := []string{}
36+
for i := 0; i < len(strR); i++ {
37+
strArr = append(strArr, string(strR[i]))
38+
}
39+
sort.Strings(strArr)
40+
return strings.Join(strArr, "")
41+
}
42+
43+
// @lc code=end

Diff for: solution/1-99/0049.group-anagrams/solution_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGroupAnagrams(t *testing.T) {
8+
var ret [][]string
9+
var strs []string
10+
11+
strs = []string{"eat", "tea", "tan", "ate", "nat", "bat"}
12+
ret = [][]string{{"eat", "tea", "ate"}, {"tan", "nat"}, {"bat"}}
13+
for k, str := range groupAnagrams(strs) {
14+
for i, v := range str {
15+
if ret[k][i] != v {
16+
t.Fatalf("case fails %v\n", ret)
17+
}
18+
}
19+
}
20+
}

Diff for: solution/1-99/_sidebar.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
- [46. 全排列](solution/1-99/0046.permutations/)
5555
- [47. 全排列 ii](solution/1-99/0047.permutations-ii/)
5656
- [48. 旋转图像](solution/1-99/0048.rotate-image/)
57-
- [49. 字母异位词分组](solution/1-99/0049.group-anagrams/)
57+
- [49. 字母异位词分组](solution/1-99/0049.group-anagrams/)
5858
- [50. pow(x, n)](solution/1-99/0050.powx-c-n/)
5959
- [51. n皇后](solution/1-99/0051.n-queens/)
6060
- [52. n皇后 ii](solution/1-99/0052.n-queens-ii/)
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# [187. 重复的DNA序列](https://leetcode-cn.com/problems/repeated-dna-sequences/description/)
2+
3+
### 题目描述
4+
5+
<p>所有 DNA 都由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:&ldquo;ACGAATTCCG&rdquo;。在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助。</p>
6+
7+
<p>编写一个函数来查找目标子串,目标子串的长度为 10,且在 DNA 字符串 <code>s</code> 中出现次数超过一次。</p>
8+
9+
<p>&nbsp;</p>
10+
11+
<p><strong>示例:</strong></p>
12+
13+
<pre><strong>输入:</strong>s = &quot;AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT&quot;
14+
<strong>输出:</strong>[&quot;AAAAACCCCC&quot;, &quot;CCCCCAAAAA&quot;]</pre>
15+
16+
### 解题思路
17+
18+
1. hash table
19+
20+
### 具体解法
21+
22+
23+
#### **Golang**
24+
```go
25+
func findRepeatedDnaSequences(s string) []string {
26+
sMap := make(map[string]bool)
27+
var res []string
28+
for i := 0; i < len(s)-9; i++ {
29+
if _, ok := sMap[s[i:i+10]]; ok {
30+
if sMap[s[i:i+10]] == true {
31+
res = append(res, s[i:i+10])
32+
}
33+
sMap[s[i:i+10]] = false
34+
} else {
35+
sMap[s[i:i+10]] = true
36+
}
37+
}
38+
return res
39+
}
40+
41+
```
42+
43+
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=187 lang=golang
5+
*
6+
* [187] 重复的DNA序列
7+
*/
8+
9+
// @lc code=start
10+
func findRepeatedDnaSequences(s string) []string {
11+
sMap := make(map[string]bool)
12+
var res []string
13+
for i := 0; i < len(s)-9; i++ {
14+
if _, ok := sMap[s[i:i+10]]; ok {
15+
if sMap[s[i:i+10]] == true {
16+
res = append(res, s[i:i+10])
17+
}
18+
sMap[s[i:i+10]] = false
19+
} else {
20+
sMap[s[i:i+10]] = true
21+
}
22+
}
23+
return res
24+
}
25+
26+
// @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestFindRepeatedDnaSequences(t *testing.T) {
8+
var ret []string
9+
var s string
10+
11+
s = "AAAAAAAAAAA"
12+
ret = []string{"AAAAAAAAAA"}
13+
for i, v := range findRepeatedDnaSequences(s) {
14+
if ret[i] != v {
15+
t.Fatalf("case fails %v\n", ret)
16+
}
17+
}
18+
19+
s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
20+
ret = []string{"AAAAACCCCC", "CCCCCAAAAA"}
21+
for i, v := range findRepeatedDnaSequences(s) {
22+
if ret[i] != v {
23+
t.Fatalf("case fails %v\n", ret)
24+
}
25+
}
26+
27+
}

Diff for: solution/100-199/_sidebar.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
- [184. 部门工资最高的员工](solution/100-199/0184.department-highest-salary/)
9494
- [185. 部门工资前三高的所有员工](solution/100-199/0185.department-top-three-salaries/)
9595
- [186. 翻转字符串里的单词 ii](solution/100-199/0186.reverse-words-in-a-string-ii/)
96-
- [187. 重复的dna序列](solution/100-199/0187.repeated-dna-sequences/)
96+
- [187. 重复的dna序列](solution/100-199/0187.repeated-dna-sequences/)
9797
- [188. 买卖股票的最佳时机 iv](solution/100-199/0188.best-time-to-buy-and-sell-stock-iv/)
9898
- [189. 旋转数组 ✅](solution/100-199/0189.rotate-array/)
9999
- [190. 颠倒二进制位](solution/100-199/0190.reverse-bits/)

Diff for: solution/200-299/0204.count-primes/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# [204. 计数质数](https://leetcode-cn.com/problems/count-primes/description/)
2+
3+
### 题目描述
4+
5+
<p>统计所有小于非负整数&nbsp;<em>n&nbsp;</em>的质数的数量。</p>
6+
7+
<p><strong>示例:</strong></p>
8+
9+
<pre><strong>输入:</strong> 10
10+
<strong>输出:</strong> 4
11+
<strong>解释:</strong> 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。
12+
</pre>
13+
14+
### 解题思路
15+
16+
1. hash table
17+
2. i的倍数不可能为素数
18+
19+
### 具体解法
20+
21+
22+
#### **Golang**
23+
```go
24+
func countPrimes(n int) int {
25+
if n < 3 {
26+
return 0
27+
}
28+
var count int
29+
fMap := make(map[int]bool)
30+
for i := 2; i < n; i++ {
31+
if fMap[i] {
32+
continue
33+
}
34+
count++
35+
fMap[i] = false
36+
// i的倍数不可能为素数
37+
for j := 2 * i; j < n; j += i {
38+
fMap[j] = true
39+
}
40+
}
41+
return count
42+
}
43+
```
44+
45+

Diff for: solution/200-299/0204.count-primes/solution.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=204 lang=golang
5+
*
6+
* [204] 计数质数
7+
*/
8+
9+
// @lc code=start
10+
func countPrimes(n int) int {
11+
if n < 3 {
12+
return 0
13+
}
14+
var count int
15+
fMap := make(map[int]bool)
16+
for i := 2; i < n; i++ {
17+
if fMap[i] {
18+
continue
19+
}
20+
count++
21+
fMap[i] = false
22+
// i的倍数不可能为素数
23+
for j := 2 * i; j < n; j += i {
24+
fMap[j] = true
25+
}
26+
}
27+
return count
28+
}
29+
30+
// @lc code=end

Diff for: solution/200-299/0204.count-primes/solution_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestCountPrimes(t *testing.T) {
8+
var ret int
9+
var n int
10+
ret = 4
11+
n = 10
12+
if ret != countPrimes(n) {
13+
t.Fatalf("case fails %v\n", ret)
14+
}
15+
16+
ret = 0
17+
n = 2
18+
if ret != countPrimes(n) {
19+
t.Fatalf("case fails %v\n", ret)
20+
}
21+
22+
}

0 commit comments

Comments
 (0)