Skip to content

Commit d123fa3

Browse files
aQuaaQua
aQua
authored and
aQua
committed
121 & 122 add
1 parent 09307dd commit d123fa3

File tree

7 files changed

+147
-3
lines changed

7 files changed

+147
-3
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# [121. Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)
2+
3+
## 题目
4+
Say you have an array for which the ith element is the price of a given stock on day i.
5+
6+
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
7+
```
8+
Example 1:
9+
Input: [7, 1, 5, 3, 6, 4]
10+
Output: 5
11+
```
12+
max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
13+
14+
```
15+
Example 2:
16+
Input: [7, 6, 4, 3, 1]
17+
Output: 0
18+
```
19+
In this case, no transaction is done, i.e. max profit = 0.
20+
21+
## 解题思路
22+
23+
见程序注释
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package Problem0121
2+
3+
func maxProfit(prices []int) int {
4+
5+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package Problem0121
2+
3+
import (
4+
"testing"
5+
"fmt"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type question struct {
11+
para
12+
ans
13+
}
14+
15+
// para 是参数
16+
type para struct {
17+
prices []int
18+
}
19+
20+
// ans 是答案
21+
type ans struct {
22+
one int
23+
}
24+
25+
func Test_Problem0121(t *testing.T) {
26+
ast := assert.New(t)
27+
28+
qs := []question{
29+
30+
question{
31+
para{
32+
,
33+
},
34+
ans{
35+
,
36+
},
37+
},
38+
39+
// 如需多个测试,可以复制上方元素。
40+
}
41+
42+
for _, q := range qs {
43+
a, p := q.ans, q.para
44+
fmt.Printf("~~%v~~\n", p)
45+
46+
ast.Equal(a.one, maxProfit(p. ), "输入:%v", p)
47+
}
48+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# [122. Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)
2+
3+
## 题目
4+
5+
Say you have an array for which the ith element is the price of a given stock on day i.
6+
7+
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
8+
9+
10+
## 解题思路
11+
12+
13+
见程序注释
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package Problem0122
2+
3+
func maxProfit(prices []int) int {
4+
5+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package Problem0122
2+
3+
import (
4+
"testing"
5+
"fmt"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type question struct {
11+
para
12+
ans
13+
}
14+
15+
// para 是参数
16+
type para struct {
17+
prices []int
18+
}
19+
20+
// ans 是答案
21+
type ans struct {
22+
one int
23+
}
24+
25+
func Test_Problem0122(t *testing.T) {
26+
ast := assert.New(t)
27+
28+
qs := []question{
29+
30+
question{
31+
para{
32+
,
33+
},
34+
ans{
35+
,
36+
},
37+
},
38+
39+
// 如需多个测试,可以复制上方元素。
40+
}
41+
42+
for _, q := range qs {
43+
a, p := q.ans, q.para
44+
fmt.Printf("~~%v~~\n", p)
45+
46+
ast.Equal(a.one, maxProfit(p.prices ), "输入:%v", p)
47+
}
48+
}

helper.v3/problem.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,13 @@ func (p problem) build() {
5151
}
5252
fc, fcHead, para, ans := getFunction(p.link())
5353

54+
log.Printf("开始创建 %d.%s 的文件夹...\n", p.ID, p.Title)
55+
5456
creatREADME(p)
5557
creatGo(p, fc)
5658
creatGoTest(p, fcHead, para, ans)
59+
60+
log.Printf("%d.%s 的文件夹,创建完毕。\n", p.ID, p.Title)
5761
}
5862

5963
func creatREADME(p problem) {
@@ -68,8 +72,6 @@ func creatREADME(p problem) {
6872
见程序注释
6973
`
7074

71-
log.Printf("正在下载 %d.%s 的问题描述\n", p.ID, p.Title)
72-
7375
questionDescription := getQuestionDescription(p.link())
7476

7577
content := fmt.Sprintf(fileFormat, p.ID, p.Title, p.link(), questionDescription)
@@ -87,7 +89,7 @@ func getQuestionDescription(URL string) string {
8789
if err != nil {
8890
log.Fatal(err)
8991
}
90-
return doc.Find("div.question-description").Text()
92+
return strings.TrimSpace(doc.Find("div.question-description").Text())
9193
}
9294

9395
func getFunction(URL string) (fc, fcHead, p, a string) {

0 commit comments

Comments
 (0)