Skip to content

Commit ed4b793

Browse files
aQuaaQua
aQua
authored and
aQua
committed
更新README.md
1 parent f787f4d commit ed4b793

File tree

6 files changed

+36
-24
lines changed

6 files changed

+36
-24
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
|565|[Array Nesting](./Algorithms/0565.array-nesting)|☆ ☆|49%||
158158
|566|[Reshape the Matrix](./Algorithms/0566.reshape-the-matrix)||58%||
159159
|581|[Shortest Unsorted Continuous Subarray](./Algorithms/0581.shortest-unsorted-continuous-subarray)||29%||
160-
|605|[Can Place Flowers](./Algorithms/0605.can-place-flowers)||30%||
160+
|605|[Can Place Flowers](./Algorithms/0605.can-place-flowers)||29%||
161161
|611|[Valid Triangle Number](./Algorithms/0611.valid-triangle-number)|☆ ☆|41%||
162162
|621|[Task Scheduler](./Algorithms/0621.task-scheduler)|☆ ☆|42%||
163163
|628|[Maximum Product of Three Numbers](./Algorithms/0628.maximum-product-of-three-numbers)||45%||

helper.v4/data.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package main
22

33
import (
44
"encoding/json"
5+
"errors"
6+
"fmt"
57
"io/ioutil"
68
"log"
79
"sort"
@@ -11,8 +13,13 @@ import (
1113

1214
func update(categories []string) *leetcode {
1315
newLC := lastest(categories)
14-
oldLC := readFile()
15-
diff(newLC, oldLC)
16+
oldLC, err := readLeetCodeRecord()
17+
if err != nil {
18+
log.Println("LeetCode 记录读取失败,无法与新记录对比:", err)
19+
} else {
20+
diff(newLC, oldLC)
21+
}
22+
1623
saveLC(newLC)
1724

1825
return newLC
@@ -33,20 +40,20 @@ func lastest(categories []string) *leetcode {
3340
return lc
3441
}
3542

36-
func readFile() *leetcode {
37-
lc := leetcode{}
43+
func readLeetCodeRecord() (*leetcode, error) {
3844
if !GoKit.Exist(lcFile) {
39-
log.Printf("%s 不存在", lcFile)
40-
return &lc
45+
msg := fmt.Sprintf("%s 不存在", lcFile)
46+
return nil, errors.New(msg)
4147
}
4248

4349
raw := read(lcFile)
44-
50+
lc := leetcode{}
4551
if err := json.Unmarshal(raw, &lc); err != nil {
46-
log.Fatalf("获取 %s 失败:%s", lcFile, err)
52+
msg := fmt.Sprintf("获取 %s 失败:%s", lcFile, err)
53+
return nil, errors.New(msg)
4754
}
4855

49-
return &lc
56+
return &lc, nil
5057
}
5158

5259
func diff(new, old *leetcode) {
@@ -101,6 +108,8 @@ func saveLC(lc *leetcode) {
101108
if err = ioutil.WriteFile(lcFile, raw, 0666); err != nil {
102109
log.Fatal("无法把Marshal后的lc保存到文件: ", err)
103110
}
111+
112+
log.Println("最新的 LeetCode 记录已经保存。")
104113
}
105114

106115
// data 保存API信息

helper.v4/main.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package main
33
import (
44
"fmt"
55
"log"
6+
"net/http"
67
"os"
78
"strconv"
89

910
"github.com/BurntSushi/toml"
11+
"github.com/mozillazg/request"
1012
)
1113

1214
// 程序辅助设置
@@ -28,6 +30,9 @@ func init() {
2830
log.Fatalf(err.Error())
2931
}
3032
log.Printf("Hi, %s. \n", cfg.Login)
33+
34+
// 对 req 赋值
35+
req = request.NewRequest(new(http.Client))
3136
}
3237

3338
func main() {
@@ -49,7 +54,10 @@ func main() {
4954

5055
// 下载题目资料不需要登录 leetcode
5156
if problemNum > 0 {
52-
lc := readFile()
57+
lc, err := readLeetCodeRecord()
58+
if err != nil {
59+
log.Fatalln("读取 LeetCode 记录失败: ", err)
60+
}
5361
makeProblemDir(lc.Problems, problemNum)
5462
return
5563
}

helper.v4/net.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"io/ioutil"
77
"log"
8-
"net/http"
98
"regexp"
109
"strings"
1110

@@ -24,7 +23,6 @@ func signin() {
2423
log.Println("正在登录中...")
2524

2625
// 配置request
27-
req = request.NewRequest(new(http.Client))
2826
req.Headers = map[string]string{
2927
"Accept-Encoding": "",
3028
"Referer": "https://leetcode.com/",
@@ -89,12 +87,12 @@ func getRaw(URL string) []byte {
8987
log.Printf("开始下载 %s 的数据", URL)
9088
resp, err := req.Get(URL)
9189
if err != nil {
92-
log.Fatal("getJSON: Get Error: " + err.Error())
90+
log.Fatal("getRaw: Get Error: " + err.Error())
9391
}
9492

9593
body, err := ioutil.ReadAll(resp.Body)
9694
if err != nil {
97-
log.Fatal("getJSON: Read Error: " + err.Error())
95+
log.Fatal("getRaw: Read Error: " + err.Error())
9896
}
9997
return body
10098
}

helper.v4/problem.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
func makeProblemDir(ps problems, problemNum int) {
1717
var pb problem
1818
var isFound bool
19+
20+
// 根据题号,获取题目信息
1921
for _, p := range ps {
2022
if p.ID == problemNum {
2123
pb = p
@@ -25,21 +27,16 @@ func makeProblemDir(ps problems, problemNum int) {
2527
}
2628

2729
if !isFound {
28-
log.Printf("没有发现 %d 题,此题不存在,或者需要付费,或者不在已关注的种类中。", problemNum)
29-
return
30-
}
31-
32-
if pb.IsAccepted && GoKit.Exist(pb.Dir) {
33-
log.Printf("第 %d 题已经accepted,请删除 %s 文件夹后,再尝试。", pb.ID, pb.Dir)
30+
log.Printf("没有发现 %d 题,存在以下可能:1.此题不存在;2.需要付费;3.不在已关注的种类中。", problemNum)
3431
return
3532
}
3633

3734
pb.build()
3835
}
3936

4037
func (p problem) build() {
41-
if err := os.RemoveAll(p.Dir); err != nil {
42-
log.Fatalln("无法删除目录", p.Dir)
38+
if p.IsAccepted && GoKit.Exist(p.Dir) {
39+
log.Fatalf("第 %d 题已经accepted,请**删除**或**重命名** %s 文件夹后,再尝试。", p.ID, p.Dir)
4340
}
4441

4542
mask := syscall.Umask(0)

leetcode.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)