Skip to content

Commit 077fa23

Browse files
Forge YaoForge Yao
authored andcommitted
commit 119/121
1 parent 1a1f161 commit 077fa23

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

119/119-getRow.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// https://leetcode-cn.com/problems/pascals-triangle-ii/
2+
package main
3+
4+
import "fmt"
5+
6+
func getRow(rowIndex int) []int {
7+
// 构造完整的杨辉三角形. 二维数组
8+
/*num := make([][]int, rowIndex+1)
9+
for i := 0; i <= rowIndex; i++ {
10+
num[i] = make([]int, i+1)
11+
num[i][0] = 1
12+
num[i][i] = 1
13+
for j := 1; j < i; j++ {
14+
num[i][j] = num[i-1][j-1] + num[i-1][j]
15+
}
16+
}
17+
return num[rowIndex]*/
18+
19+
// 节省空间版。一维数组
20+
num := make([]int, rowIndex+1)
21+
num[0] = 1
22+
for i := 1; i <= rowIndex; i++ {
23+
num[i] = 1
24+
pre := num[0]
25+
for j := 1; j < i; j++ {
26+
cur := num[j]
27+
num[j] = pre + cur
28+
pre = cur
29+
}
30+
}
31+
return num
32+
}
33+
34+
func main() {
35+
n := []int{3, 4, 5}
36+
for i := 0; i < len(n); i++ {
37+
fmt.Println("ret:", getRow(n[i]))
38+
}
39+
}

121/121-maxProfit.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/
2+
package main
3+
4+
import "fmt"
5+
6+
func maxProfit2(prices []int) int {
7+
length := len(prices)
8+
min, max, diff := 0, 0, 0
9+
for i := 1; i < length; {
10+
if prices[min] >= prices[i] {
11+
min = i
12+
i++
13+
continue
14+
} else {
15+
max = i
16+
for j := i + 1; j < length; j++ {
17+
if prices[j] > prices[max] {
18+
max = j
19+
}
20+
}
21+
for j := i + 1; j < max; j++ {
22+
if prices[min] > prices[j] {
23+
min = j
24+
}
25+
}
26+
}
27+
if max > min && prices[max]-prices[min] > diff {
28+
diff = prices[max] - prices[min]
29+
}
30+
i = max + 2
31+
min = max + 1
32+
max = max + 1
33+
}
34+
return diff
35+
}
36+
37+
func maxProfit(prices []int) int {
38+
min, maxDiff := prices[0], 0
39+
for i := 1; i < len(prices); i++ {
40+
if prices[i]-min > maxDiff {
41+
maxDiff = prices[i] - min
42+
}
43+
if prices[i] < min {
44+
min = prices[i]
45+
}
46+
}
47+
return maxDiff
48+
}
49+
50+
func main() {
51+
n := [][]int{{7, 1, 5, 3, 6, 4}, {7, 6, 4, 3, 1}, {1, 2, 3, 4, 5}, {4, 5, 6, 7, 1, 5}, {4, 5, 2, 7, 8}}
52+
for i := 0; i < len(n); i++ {
53+
fmt.Println("ret:", maxProfit(n[i]))
54+
}
55+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ LeetCode
3535
|111|[二叉树的最小深度](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/)|[Go](./111/111-minDepth.go)|简单|二叉树|
3636
|112|[路径总和](https://leetcode-cn.com/problems/path-sum/)|[Go](./112/112-hasPathSum.go)|简单|二叉树|
3737
|118|[杨辉三角](https://leetcode-cn.com/problems/pascals-triangle/)|[Go](./118/118-generate.go)|简单||
38+
|119|[杨辉三角 II](https://leetcode-cn.com/problems/pascals-triangle-ii/)|[Go](./119/119-getRow.go)|简单||
39+
|121|[买卖股票的最佳时机](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/)|[Go](./121/121-maxProfit.go)|简单||
3840
|125|[验证回文串](https://leetcode-cn.com/problems/valid-palindrome/)|[Go](./125-isPalindrome/125-isPalindrome.go)|简单||
3941
|131|[分割回文串](https://leetcode-cn.com/problems/palindrome-partitioning/) | [Go](./131-partition/131-partition.go)|中等|递归, 动态规划|
4042
|141|[环形链表](https://leetcode-cn.com/problems/linked-list-cycle/)|[Go](./141-hasCycle/141-hasCycle.go)|简单|双指针|

0 commit comments

Comments
 (0)