Skip to content

Commit 9df3022

Browse files
committed
Merge branch 'master' into janetyu
2 parents aa5ef0b + d0543da commit 9df3022

File tree

8 files changed

+242
-561
lines changed

8 files changed

+242
-561
lines changed

README.md

Lines changed: 26 additions & 541 deletions
Large diffs are not rendered by default.

leetcode/0003.Longest-Substring-Without-Repeating-Characters/3. Longest Substring Without Repeating Characters.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@ func lengthOfLongestSubstring(s string) int {
55
if len(s) == 0 {
66
return 0
77
}
8-
// 扩展 ASCII 码的位图表示(BitSet),共有 256 位
9-
var bitSet [256]uint8
8+
var bitSet [256]bool
109
result, left, right := 0, 0, 0
1110
for left < len(s) {
12-
if right < len(s) && bitSet[s[right]] == 0 {
13-
// 标记对应的 ASCII 码为 1
14-
bitSet[s[right]] = 1
15-
right++
16-
} else {
17-
// 标记对应的 ASCII 码为 0
18-
bitSet[s[left]] = 0
11+
// 右侧字符对应的bitSet被标记true,说明此字符在X位置重复,需要左侧向前移动,直到将X标记为false
12+
if bitSet[s[right]] {
13+
bitSet[s[left]] = false
1914
left++
15+
} else {
16+
bitSet[s[right]] = true
17+
right++
18+
}
19+
if result < right-left {
20+
result = right - left
21+
}
22+
if left+result >= len(s) || right >= len(s) {
23+
break
2024
}
21-
result = max(result, right-left)
2225
}
2326
return result
2427
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package leetcode
2+
3+
func generate(numRows int) [][]int {
4+
result := [][]int{}
5+
for i := 0; i < numRows; i++ {
6+
row := []int{}
7+
for j := 0; j < i+1; j++ {
8+
if j == 0 || j == i {
9+
row = append(row, 1)
10+
} else if i > 1 {
11+
row = append(row, result[i-1][j-1]+result[i-1][j])
12+
}
13+
}
14+
result = append(result, row)
15+
}
16+
return result
17+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question118 struct {
9+
para118
10+
ans118
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para118 struct {
16+
numRows int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans118 struct {
22+
one [][]int
23+
}
24+
25+
func Test_Problem118(t *testing.T) {
26+
27+
qs := []question118{
28+
29+
question118{
30+
para118{2},
31+
ans118{[][]int{{1}, {1, 1}}},
32+
},
33+
34+
question118{
35+
para118{5},
36+
ans118{[][]int{{1}, {1, 1}, {1, 2, 1}, {1, 3, 3, 1}, {1, 4, 6, 4, 1}}},
37+
},
38+
39+
question118{
40+
para118{10},
41+
ans118{[][]int{{1}, {1, 1}, {1, 2, 1}, {1, 3, 3, 1}, {1, 4, 6, 4, 1}, {1, 5, 10, 10, 5, 1}, {1, 6, 15, 20, 15, 6, 1}, {1, 7, 21, 35, 35, 21, 7, 1}, {1, 8, 28, 56, 70, 56, 28, 8, 1}, {1, 9, 36, 84, 126, 126, 84, 36, 9, 1}}},
42+
},
43+
}
44+
45+
fmt.Printf("------------------------Leetcode Problem 118------------------------\n")
46+
47+
for _, q := range qs {
48+
_, p := q.ans118, q.para118
49+
fmt.Printf("【input】:%v 【output】:%v\n", p, generate(p.numRows))
50+
}
51+
fmt.Printf("\n\n\n")
52+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# [118. Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)
2+
3+
4+
## 题目
5+
6+
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
7+
8+
![](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)
9+
10+
**Note:** In Pascal's triangle, each number is the sum of the two numbers directly above it.
11+
12+
**Example:**
13+
14+
```
15+
Input: 5
16+
Output:
17+
[
18+
[1],
19+
[1,1],
20+
[1,2,1],
21+
[1,3,3,1],
22+
[1,4,6,4,1]
23+
]
24+
```
25+
26+
## 题目大意
27+
28+
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。在杨辉三角中,每个数是它左上方和右上方的数的和。
29+
30+
31+
## 解题思路
32+
33+
- 给定一个 n,要求打印杨辉三角的前 n 行。
34+
- 简单题。按照杨辉三角的生成规则循环打印即可。
35+
36+
37+
## 代码
38+
39+
```go
40+
41+
package leetcode
42+
43+
func generate(numRows int) [][]int {
44+
result := [][]int{}
45+
for i := 0; i < numRows; i++ {
46+
row := []int{}
47+
for j := 0; j < i+1; j++ {
48+
if j == 0 || j == i {
49+
row = append(row, 1)
50+
} else if i > 1 {
51+
row = append(row, result[i-1][j-1]+result[i-1][j])
52+
}
53+
}
54+
result = append(result, row)
55+
}
56+
return result
57+
}
58+
59+
```
60+

website/content/ChapterFour/0003.Longest-Substring-Without-Repeating-Characters.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,23 @@ func lengthOfLongestSubstring(s string) int {
6060
if len(s) == 0 {
6161
return 0
6262
}
63-
// 扩展 ASCII 码的位图表示(BitSet),共有 256 位
64-
var bitSet [256]uint8
63+
var bitSet [256]bool
6564
result, left, right := 0, 0, 0
6665
for left < len(s) {
67-
if right < len(s) && bitSet[s[right]] == 0 {
68-
// 标记对应的 ASCII 码为 1
69-
bitSet[s[right]] = 1
70-
right++
71-
} else {
72-
// 标记对应的 ASCII 码为 0
73-
bitSet[s[left]] = 0
66+
// 右侧字符对应的 bitSet 被标记 true,说明此字符在 X 位置重复,需要左侧向前移动,直到将X标记为 false
67+
if bitSet[s[right]] {
68+
bitSet[s[left]] = false
7469
left++
70+
} else {
71+
bitSet[s[right]] = true
72+
right++
73+
}
74+
if result < right-left {
75+
result = right - left
76+
}
77+
if left+result >= len(s) || right >= len(s) {
78+
break
7579
}
76-
result = max(result, right-left)
7780
}
7881
return result
7982
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# [118. Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)
2+
3+
4+
## 题目
5+
6+
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
7+
8+
![](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)
9+
10+
**Note:** In Pascal's triangle, each number is the sum of the two numbers directly above it.
11+
12+
**Example:**
13+
14+
```
15+
Input: 5
16+
Output:
17+
[
18+
[1],
19+
[1,1],
20+
[1,2,1],
21+
[1,3,3,1],
22+
[1,4,6,4,1]
23+
]
24+
```
25+
26+
## 题目大意
27+
28+
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。在杨辉三角中,每个数是它左上方和右上方的数的和。
29+
30+
31+
## 解题思路
32+
33+
- 给定一个 n,要求打印杨辉三角的前 n 行。
34+
- 简单题。按照杨辉三角的生成规则循环打印即可。
35+
36+
37+
## 代码
38+
39+
```go
40+
41+
package leetcode
42+
43+
func generate(numRows int) [][]int {
44+
result := [][]int{}
45+
for i := 0; i < numRows; i++ {
46+
row := []int{}
47+
for j := 0; j < i+1; j++ {
48+
if j == 0 || j == i {
49+
row = append(row, 1)
50+
} else if i > 1 {
51+
row = append(row, result[i-1][j-1]+result[i-1][j])
52+
}
53+
}
54+
result = append(result, row)
55+
}
56+
return result
57+
}
58+
59+
```
60+

website/content/menu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ headless: true
125125
- [0112.Path-Sum]({{< relref "/ChapterFour/0112.Path-Sum.md" >}})
126126
- [0113.Path-Sum-II]({{< relref "/ChapterFour/0113.Path-Sum-II.md" >}})
127127
- [0114.Flatten-Binary-Tree-to-Linked-List]({{< relref "/ChapterFour/0114.Flatten-Binary-Tree-to-Linked-List.md" >}})
128+
- [0118.Pascals-Triangle]({{< relref "/ChapterFour/0118.Pascals-Triangle.md" >}})
128129
- [0120.Triangle]({{< relref "/ChapterFour/0120.Triangle.md" >}})
129130
- [0121.Best-Time-to-Buy-and-Sell-Stock]({{< relref "/ChapterFour/0121.Best-Time-to-Buy-and-Sell-Stock.md" >}})
130131
- [0122.Best-Time-to-Buy-and-Sell-Stock-II]({{< relref "/ChapterFour/0122.Best-Time-to-Buy-and-Sell-Stock-II.md" >}})

0 commit comments

Comments
 (0)