Skip to content

Commit 5ce8ea5

Browse files
committed
[E:10/528, M:8/965, H:0/384] add No: 795 K-th Symbol in Grammar
1 parent 6b9d67f commit 5ce8ea5

File tree

13 files changed

+400
-6
lines changed

13 files changed

+400
-6
lines changed

cmd/command/get.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ import (
1111
)
1212

1313
var getCmd = &cobra.Command{
14-
Use: "get question_id|leetcode_url",
15-
Short: "get leet question from leetcode-cn.com",
16-
Example: "leet get 222",
14+
Use: "get question_id|leetcode_url",
15+
Short: "get leetcode question from leetcode-cn.com",
16+
Example: `
17+
leetcode get 795
18+
leetcode get leetcode-cn.com/problems/k-th-symbol-in-grammar
19+
leetcode get https://leetcode-cn.com/problems/k-th-symbol-in-grammar
20+
leetcode get https://leetcode-cn.com/problems/k-th-symbol-in-grammar/solution/
21+
`,
1722
Run: func(cmd *cobra.Command, args []string) {
1823

1924
if len(args) != 1 {
@@ -33,6 +38,12 @@ var getCmd = &cobra.Command{
3338
}
3439

3540
param := leet.Parse(strings.TrimSpace(args[0]))
41+
if param == "" {
42+
cmd.Println(fmt.Sprintf("未找到关于 「%s」 的相关题目", args[0]))
43+
cmd.Help()
44+
os.Exit(1)
45+
return
46+
}
3647

3748
res, err := leet.Fetch(param)
3849
if err != nil {

cmd/command/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
var infoCmd = &cobra.Command{
1414
Use: "info question_id|leetcode_url",
15-
Short: "print leet question info",
15+
Short: "print leetcode question info",
1616
Run: func(cmd *cobra.Command, args []string) {
1717

1818
if len(args) != 1 {

leet/leetcode.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ const (
5353
cacheDuration = time.Hour * 24
5454
)
5555

56+
// Parse ...
57+
// param: 222
58+
// param: https://leetcode-cn.com/problems/k-th-symbol-in-grammar
59+
// param: https://leetcode-cn.com/problems/k-th-symbol-in-grammar/solution/
60+
// param: leetcode-cn.com/problems/k-th-symbol-in-grammar
5661
func Parse(param string) string {
5762
if match, err := regexp.MatchString(`\d+`, param); err == nil && match {
5863
stat, err := ProblemID2name(param)
@@ -61,20 +66,34 @@ func Parse(param string) string {
6166
}
6267
}
6368

69+
return ParseFromURL(param)
70+
}
71+
72+
// ParseFromURL
73+
// param: https://leetcode-cn.com/problems/k-th-symbol-in-grammar
74+
// param: https://leetcode-cn.com/problems/k-th-symbol-in-grammar/solution/
75+
// param: leetcode-cn.com/problems/k-th-symbol-in-grammar
76+
func ParseFromURL(param string) string {
6477
if strings.HasPrefix(param, "http") ||
6578
strings.HasPrefix(param, "leetcode-cn.com") {
6679

6780
re := regexp.MustCompile(`leetcode-cn\.com/problems/(.*)`)
6881

6982
result := re.FindStringSubmatch(param)
7083
if len(result) == 2 {
71-
return strings.Trim(result[1], "/")
84+
result := strings.Trim(result[1], "/")
85+
ss := strings.Split(result, "/")
86+
if len(ss) == 2 {
87+
result = ss[0]
88+
}
89+
return result
7290
}
7391
}
7492

7593
return ""
7694
}
7795

96+
// ProblemID2name return problem info
7897
func ProblemID2name(id string) (stats QuestionStats, err error) {
7998
allProblemsDir := path.Join("questions", "all_problems.json")
8099
// 文件是否存在 / 结果是否过期

leet/leetcode_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package leet
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestParseFromURL(t *testing.T) {
8+
type args struct {
9+
param string
10+
}
11+
tests := []struct {
12+
name string
13+
args args
14+
want string
15+
}{
16+
{
17+
name: "solution1",
18+
args: args{
19+
param: "https://leetcode-cn.com/problems/k-th-symbol-in-grammar/solution/",
20+
},
21+
want: "k-th-symbol-in-grammar",
22+
},
23+
{
24+
name: "solution2",
25+
args: args{
26+
param: "leetcode-cn.com/problems/k-th-symbol-in-grammar/solution/",
27+
},
28+
want: "k-th-symbol-in-grammar",
29+
},
30+
{
31+
name: "solution3",
32+
args: args{
33+
param: "https://leetcode-cn.com/problems/k-th-symbol-in-grammar/",
34+
},
35+
want: "k-th-symbol-in-grammar",
36+
},
37+
{
38+
name: "solution4",
39+
args: args{
40+
param: "https://leetcode-cn.com/problems/k-th-symbol-in-grammar",
41+
},
42+
want: "k-th-symbol-in-grammar",
43+
},
44+
{
45+
name: "solution5",
46+
args: args{
47+
param: "leetcode-cn.com/problems/k-th-symbol-in-grammar",
48+
},
49+
want: "k-th-symbol-in-grammar",
50+
},
51+
{
52+
name: "solution6",
53+
args: args{
54+
param: "leetcode-cn.com/problems/k-th-symbol-in-grammar/",
55+
},
56+
want: "k-th-symbol-in-grammar",
57+
},
58+
}
59+
for _, tt := range tests {
60+
t.Run(tt.name, func(t *testing.T) {
61+
if got := ParseFromURL(tt.args.param); got != tt.want {
62+
t.Errorf("ParseFromURL() = %v, want %v", got, tt.want)
63+
}
64+
})
65+
}
66+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/中等/795/golang/solution"
11+
)
12+
13+
func main() {
14+
15+
tests := []struct {
16+
name string
17+
input1 int
18+
input2 int
19+
want int
20+
}{
21+
{
22+
name: "test-1-1",
23+
input1: 1,
24+
input2: 1,
25+
want: 0,
26+
},
27+
{
28+
name: "test-2-1",
29+
input1: 2,
30+
input2: 1,
31+
want: 0,
32+
},
33+
{
34+
name: "test-2-2",
35+
input1: 2,
36+
input2: 2,
37+
want: 1,
38+
},
39+
{
40+
name: "test-3-2",
41+
input1: 3,
42+
input2: 2,
43+
want: 1,
44+
},
45+
{
46+
name: "test-3-3",
47+
input1: 3,
48+
input2: 3,
49+
want: 1,
50+
},
51+
{
52+
name: "test-4-5",
53+
input1: 4,
54+
input2: 5,
55+
want: 1,
56+
},
57+
{
58+
name: "test-4-7",
59+
input1: 4,
60+
input2: 7,
61+
want: 0,
62+
},
63+
}
64+
65+
testLog := leet.NewTestLog(len(tests))
66+
defer testLog.Render()
67+
68+
timeoutDuration := time.Second * 2
69+
70+
for idx, test := range tests {
71+
// 超时检测
72+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
73+
solution.Export(test.input1, test.input2)
74+
cancel()
75+
})
76+
77+
if timeout {
78+
testLog.Fail(idx+1, test.name, "timeout")
79+
continue
80+
}
81+
82+
got := solution.Export(test.input1, test.input2)
83+
if !reflect.DeepEqual(test.want, got) {
84+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
85+
continue
86+
}
87+
88+
testLog.Pass(idx+1, test.name)
89+
}
90+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(N int, K int) int {
4+
return kthGrammar(N, K)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func kthGrammar(N int, K int) int {
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(N int, K int) int {
4+
return kthGrammar(N, K)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func kthGrammar(N int, K int) int {
13+
14+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## [第K个语法符号](https://leetcode-cn.com/problems/k-th-symbol-in-grammar/)
2+
3+
在第一行我们写上一个 `0`。接下来的每一行,将前一行中的`0`替换为`01``1`替换为`10`
4+
5+
给定行数 `N` 和序数 `K`,返回第 `N` 行中第 `K`个字符。(`K`从1开始)
6+
7+
**例子:**
8+
9+
`**输入:** N = 1, K = 1
10+
**输出:** 0
11+
12+
**输入:** N = 2, K = 1
13+
**输出:** 0
14+
15+
**输入:** N = 2, K = 2
16+
**输出:** 1
17+
18+
**输入:** N = 4, K = 5
19+
**输出:** 1
20+
21+
**解释:**
22+
第一行: 0
23+
第二行: 01
24+
第三行: 0110
25+
第四行: 01101001
26+
`
27+
28+
**注意:**
29+
30+
1. `N` 的范围 `[1, 30]`.
31+
2. `K` 的范围 `[1, 2^(N-1)]`.

questions/store.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/中等/795/golang/solution"
11+
)
12+
13+
func main() {
14+
15+
tests := []struct {
16+
name string
17+
input1 int
18+
input2 int
19+
want int
20+
}{
21+
{
22+
name: "test-1-1",
23+
input1: 1,
24+
input2: 1,
25+
want: 0,
26+
},
27+
{
28+
name: "test-2-1",
29+
input1: 2,
30+
input2: 1,
31+
want: 0,
32+
},
33+
{
34+
name: "test-2-2",
35+
input1: 2,
36+
input2: 2,
37+
want: 1,
38+
},
39+
{
40+
name: "test-3-2",
41+
input1: 3,
42+
input2: 2,
43+
want: 1,
44+
},
45+
{
46+
name: "test-3-3",
47+
input1: 3,
48+
input2: 3,
49+
want: 1,
50+
},
51+
{
52+
name: "test-4-5",
53+
input1: 4,
54+
input2: 5,
55+
want: 1,
56+
},
57+
{
58+
name: "test-4-7",
59+
input1: 4,
60+
input2: 7,
61+
want: 0,
62+
},
63+
}
64+
65+
testLog := leet.NewTestLog(len(tests))
66+
defer testLog.Render()
67+
68+
timeoutDuration := time.Second * 2
69+
70+
for idx, test := range tests {
71+
// 超时检测
72+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
73+
solution.Export(test.input1, test.input2)
74+
cancel()
75+
})
76+
77+
if timeout {
78+
testLog.Fail(idx+1, test.name, "timeout")
79+
continue
80+
}
81+
82+
got := solution.Export(test.input1, test.input2)
83+
if !reflect.DeepEqual(test.want, got) {
84+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
85+
continue
86+
}
87+
88+
testLog.Pass(idx+1, test.name)
89+
}
90+
}

0 commit comments

Comments
 (0)