Skip to content

Commit 890eb07

Browse files
committed
subcommand solution is out, you can list tested solutions use leetcode solution
1 parent cee13c4 commit 890eb07

File tree

2,045 files changed

+255035
-24558
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,045 files changed

+255035
-24558
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616

1717
leetcode
1818
/solutions/
19-
questions/all_problems.json
19+
questions/all_problems.json
20+
solutions.db

README.md

Lines changed: 119 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# leetcode
2-
leetcode 本地化题库,可本地刷题/测试/调试
2+
leetcode 本地化题库,本地刷题/测试/调试/保存回放你所有解答
33

44
在线执行 golang 代码跑算法,可以使用
55
[Golang 在线代码执行](https://runcode.6cm.co/go): https://runcode.6cm.co/go
66

77
本项目只包含问题及原始网站提供的代码片断,想看题解及代码实现的可查看源站题解或者:[LeetCode-Go](https://github.com/halfrost/LeetCode-Go)
88

9-
本地刷题及单步调试算法是本仓库存在的意义
9+
本地刷题及单步调试算法、保留所有题解及查看解答变迁是本仓库存在的意义
1010

1111
## Intro
1212
本仓库是 [`leetcode-cn.com`](https://leetcode-cn.com) 本地化题库,包含本地化测试用例、常用 go 工具包的一个仓库。
@@ -16,12 +16,26 @@ leetcode 本地化题库,可本地刷题/测试/调试
1616
- leetcode 在线题目本地化 (questions 目录)
1717
- 本地化测试用例,本地调试 (cmd 目录)
1818
- 常用刷题 go 语言工具包 (utils 目录)
19+
- 保存题解,随时查看历史 (./leetcode solution 命令)
1920

2021
## 须知
2122
- 抱着怀疑的心态使用测试用例(当然每个测试用例都经过精心测试,但难免会有意外)
2223
- 每次执行测试时会先运行一次,检测是否会超时,超时默认为2s
2324
- 当前所有测试用例共用一个运行环境,需考虑算法是否会对此有特殊要求(也就是说,你写的算法块最好是无状态的,注意:全局变量会在每个测试用例间共享)
2425
- 开始你的算法之旅
26+
- 所有包含路径的部分在 goland/idea 编辑器控制台可以直接点击跳转到代码,省去找算法目录的过程
27+
28+
## 安装
29+
```shell script
30+
git --depth 1 clone https://github.com/gladmo/leetcode.git
31+
cd leetcode
32+
go build -o leetcode cmd/leetcode.go
33+
./leetcode
34+
35+
# or
36+
go install cmd/leetcode.go
37+
leetcode
38+
```
2539

2640
## 主要功能
2741
### 本地化测试
@@ -65,10 +79,100 @@ leetcode 本地化题库,可本地刷题/测试/调试
6579
+----------+------------------------------+
6680
| 标签 | 深度优先搜索,广度优先搜索,图 |
6781
+----------+------------------------------+
68-
| 难度 | 中等 |
82+
| 难度 | medium |
6983
+----------+------------------------------+
7084
```
7185

86+
所有你使用 `./leetcode test ...` 测试过的代码,都会保存到当前目录 `solutions.db`
87+
88+
可以使用 `./leetcode solution list` 列出所有解答:
89+
```shell
90+
./leetcode solution list
91+
```
92+
93+
得到类似如下输出:
94+
```
95+
+--------+----------------------+------+--------+----------+------------+------------+
96+
| 问题ID | 标题 | 难度 | 版本数 | 测试次数 | 首次提交 | 最后提交 |
97+
+--------+----------------------+------+--------+----------+------------+------------+
98+
| 1786 | 统计一致字符串的数目 | easy | 1 | 0 | 2020-12-13 | 2020-12-13 |
99+
+--------+----------------------+------+--------+----------+------------+------------+
100+
| 283 | 移动零 | easy | 1 | 0 | 2020-12-13 | 2020-12-13 |
101+
+--------+----------------------+------+--------+----------+------------+------------+
102+
```
103+
104+
获取某一题下所有测试过的题解:
105+
```shell
106+
./leetcode solution get 283
107+
```
108+
109+
得到类似以下输出:
110+
```shell
111+
+------+--------+----------+------+--------+------------+------+
112+
| 序号 | 语言 | 测试次数 | 评价 | 消耗 | 创建时间 | 备注 |
113+
+------+--------+----------+------+--------+------------+------+
114+
| 1 | golang | 0 | 6/6 | 1.387s | 2020-12-13 | |
115+
+------+--------+----------+------+--------+------------+------+
116+
```
117+
118+
查看提交的题解代码部分:
119+
```shell
120+
./leetcode solution code 283 1
121+
```
122+
123+
```shell
124+
func moveZeroes(nums []int) {
125+
var j int
126+
for i := 0; i < len(nums); i++ {
127+
if nums[i] != 0 {
128+
nums[j], nums[i] = nums[i], nums[j]
129+
j++
130+
}
131+
}
132+
}
133+
```
134+
135+
查看题解详情:
136+
```shell
137+
./leetcode solution describe 283 1
138+
```
139+
140+
```shell
141+
+-------+---------------------------------------------------------------+
142+
| 编号: | 283 |
143+
+-------+---------------------------------------------------------------+
144+
| 题目: | 移动零 |
145+
+-------+---------------------------------------------------------------+
146+
| 难度: | easy |
147+
+-------+---------------------------------------------------------------+
148+
| 语言: | golang |
149+
+-------+---------------------------------------------------------------+
150+
| 路径: | questions/serial/easy/283/golang/solution/move-zeroes.go:11:1 |
151+
+-------+---------------------------------------------------------------+
152+
| 时间: | 2020-12-13 |
153+
+-------+---------------------------------------------------------------+
154+
代码:
155+
func moveZeroes(nums []int) {
156+
var j int
157+
for i := 0; i < len(nums); i++ {
158+
if nums[i] != 0 {
159+
nums[j], nums[i] = nums[i], nums[j]
160+
j++
161+
}
162+
}
163+
}
164+
```
165+
166+
将代码检出到问题中:
167+
```shell
168+
./leetcode solution checkout 283 1
169+
170+
题目: 移动零, 编号: 283, 代码检出成功。
171+
路径: questions/serial/easy/283/golang/solution/move-zeroes.go:11:1
172+
```
173+
174+
代码检出后,将会使用历史的题解覆盖当前题目代码,方便回顾之前写算法的过程。
175+
72176
## Q & A
73177
```
74178
Q: 为何会有这个仓库?
@@ -89,6 +193,9 @@ A: 首先,如果你使用 golang 刷题时,按题号检索时, questions/s
89193
注意在测试的时候,需要使用 (leetcode test --tag [you-choose-tag] ... )
90194
需要带上 --tag [you-choose-tag]
91195

196+
Q: Goland 控制台输出的链接太难识别了怎么办?
197+
A: 可以到 Editor -> Color Scheme -> General -> Hyperlinks -> Reference 调整配色
198+
92199
Q: 我想贡献代码,我应该怎么做?
93200
A: 首先感谢你对项目的支持。
94201
1. 现有问题 markdown 或者测试用例修改
@@ -104,15 +211,7 @@ A: 首先感谢你对项目的支持。
104211
再次感谢!
105212
```
106213
107-
## 快速开始
108-
```shell script
109-
git --depth 1 clone https://github.com/gladmo/leetcode.git
110-
cd leetcode
111-
go build -o leetcode cmd/leetcode.go
112-
./leetcode
113-
```
114-
115-
你可以看到如下输出
214+
### 命令功能简介:
116215
```shell script
117216
leetcode cli
118217

@@ -123,10 +222,12 @@ Available Commands:
123222
backup backup you complete questions to solutions
124223
base clear & replace all question use you specified (backup all unbanked)
125224
clear set questions to default (backup all unbanked)
126-
get get leet question from leetcode-cn.com
225+
completion Generate completion script
226+
get get leetcode question from leetcode-cn.com
127227
help Help about any command
128-
info print leet question info
129-
test test you code and analyse
228+
info print leetcode question info
229+
solution 题解管理
230+
test 测试本地代码,保存题解
130231
version Print the version number of leetcode cli
131232

132233
Flags:
@@ -163,7 +264,7 @@ Use "leetcode [command] --help" for more information about a command.
163264
+----------+------------------------------+
164265
| 标签 | 深度优先搜索,广度优先搜索,图 |
165266
+----------+-----------------------------+
166-
| 难度 | 中等 |
267+
| 难度 | medium |
167268
+----------+------------------------------+
168269
```
169270
@@ -184,6 +285,8 @@ Use "leetcode [command] --help" for more information about a command.
184285
```
185286
186287
## TODO
288+
- [x] leetcode solution 命令,使用测试时保存的所有题解都可以用此命令及其子命令查看
187289
- [x] leetcode backup 命令
290+
- [ ] questions 目录下的两个json文件改用 bolt 存储
188291
- [ ] leetcode login
189292
- [ ] leetcode publish

cmd/command/command.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,33 @@ import (
88
)
99

1010
var (
11-
Version = "v1.0.0"
11+
Version = "v1.1.0"
1212
)
1313

1414
var cmd = &cobra.Command{
1515
Use: "leetcode",
1616
Short: "leetcode cli",
17+
ValidArgs: []string{
18+
"backup",
19+
"base",
20+
"clear",
21+
"completion",
22+
"get",
23+
"help",
24+
"info",
25+
"solution",
26+
"test",
27+
"version",
28+
},
1729
}
1830

1931
func init() {
20-
cmd.AddCommand(versionCmd, getCmd, clearCmd, infoCmd, testCmd, baseCmd, backupCmd)
32+
cmd.AddCommand(
33+
versionCmd, getCmd, clearCmd,
34+
infoCmd, testCmd, baseCmd, backupCmd,
35+
solutionCmd,
36+
completionCmd,
37+
)
2138
}
2239

2340
var versionCmd = &cobra.Command{

cmd/command/completion.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package command
2+
3+
import (
4+
"os"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var completionCmd = &cobra.Command{
10+
Use: "completion [bash|zsh|fish|powershell]",
11+
Short: "Generate completion script",
12+
Long: `To load completions:
13+
14+
Bash:
15+
16+
$ source <(leetcode completion bash)
17+
18+
# To load completions for each session, execute once:
19+
Linux:
20+
$ leetcode completion bash > /etc/bash_completion.d/leetcode
21+
MacOS:
22+
$ leetcode completion bash > /usr/local/etc/bash_completion.d/leetcode
23+
24+
Zsh:
25+
26+
# If shell completion is not already enabled in your environment you will need
27+
# to enable it. You can execute the following once:
28+
29+
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
30+
31+
# To load completions for each session, execute once:
32+
$ leetcode completion zsh > "${fpath[1]}/_leetcode"
33+
34+
# You will need to start a new shell for this setup to take effect.
35+
36+
Fish:
37+
38+
$ leetcode completion fish | source
39+
40+
# To load completions for each session, execute once:
41+
$ leetcode completion fish > ~/.config/fish/completions/leetcode.fish
42+
`,
43+
DisableFlagsInUseLine: true,
44+
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
45+
Args: cobra.ExactValidArgs(1),
46+
Run: func(cmd *cobra.Command, args []string) {
47+
switch args[0] {
48+
case "bash":
49+
cmd.Root().GenBashCompletion(os.Stdout)
50+
case "zsh":
51+
cmd.Root().GenZshCompletion(os.Stdout)
52+
case "fish":
53+
cmd.Root().GenFishCompletion(os.Stdout, true)
54+
case "powershell":
55+
cmd.Root().GenPowerShellCompletion(os.Stdout)
56+
}
57+
},
58+
}

0 commit comments

Comments
 (0)