Skip to content

Commit 423b376

Browse files
committed
[E:21/531, M:14/969, H:0/386] add No: 374 Guess Number Higher or Lower
1 parent 491c9a1 commit 423b376

File tree

10 files changed

+392
-2
lines changed

10 files changed

+392
-2
lines changed

leet/golang.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func parseGoCode(code string) (newCode string, ok bool) {
136136
if len(strings.TrimSpace(comment)) > 0 {
137137
typeIndex := strings.Index(code, `type`)
138138
commentRightParentheses := strings.Index(code, `}`)
139-
if typeIndex < commentRightParentheses {
139+
if typeIndex != -1 && typeIndex < commentRightParentheses {
140140
sc := comment[typeIndex:commentRightParentheses]
141141
for _, c := range strings.Split(sc, "\n") {
142142
structCode += "\n" + strings.Replace(strings.TrimSpace(c), "*", "", 1)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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/简单/374/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-10-6",
23+
input1: 10,
24+
input2: 6,
25+
want: 6,
26+
},
27+
{
28+
name: "test-1-1",
29+
input1: 1,
30+
input2: 1,
31+
want: 1,
32+
},
33+
{
34+
name: "test-2-1",
35+
input1: 2,
36+
input2: 1,
37+
want: 1,
38+
},
39+
{
40+
name: "test-2-2",
41+
input1: 2,
42+
input2: 2,
43+
want: 2,
44+
},
45+
}
46+
47+
testLog := leet.NewTestLog(len(tests))
48+
defer testLog.Render()
49+
50+
timeoutDuration := time.Second * 2
51+
52+
for idx, test := range tests {
53+
// 超时检测
54+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
55+
solution.Export(test.input1, test.input2)
56+
cancel()
57+
})
58+
59+
if timeout {
60+
testLog.Fail(idx+1, test.name, "timeout")
61+
continue
62+
}
63+
64+
got := solution.Export(test.input1, test.input2)
65+
if !reflect.DeepEqual(test.want, got) {
66+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
67+
continue
68+
}
69+
70+
testLog.Pass(idx+1, test.name)
71+
}
72+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package solution
2+
3+
var guessNum int
4+
5+
func Export(n, num int) int {
6+
guessNum = num
7+
return guess(n)
8+
}
9+
10+
func guess(num int) int {
11+
if num > guessNum {
12+
return -1
13+
} else if num < guessNum {
14+
return 1
15+
} else {
16+
return 0
17+
}
18+
}
19+
20+
/****************************************************/
21+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
22+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
23+
/****************************************************/
24+
25+
/**
26+
* Forward declaration of guess API.
27+
* @param num your guess
28+
* @return -1 if num is lower than the guess number
29+
* 1 if num is higher than the guess number
30+
* otherwise return 0
31+
* func guess(num int) int;
32+
*/
33+
34+
func guessNumber(n int) int {
35+
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package solution
2+
3+
var guessNum int
4+
5+
func Export(n, num int) int {
6+
guessNum = num
7+
return guess(n)
8+
}
9+
10+
func guess(num int) int {
11+
if num > guessNum {
12+
return -1
13+
} else if num < guessNum {
14+
return 1
15+
} else {
16+
return 0
17+
}
18+
}
19+
20+
/****************************************************/
21+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
22+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
23+
/****************************************************/
24+
25+
/**
26+
* Forward declaration of guess API.
27+
* @param num your guess
28+
* @return -1 if num is lower than the guess number
29+
* 1 if num is higher than the guess number
30+
* otherwise return 0
31+
* func guess(num int) int;
32+
*/
33+
34+
func guessNumber(n int) int {
35+
36+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## [猜数字大小](https://leetcode-cn.com/problems/guess-number-higher-or-lower/)
2+
3+
猜数字游戏的规则如下:
4+
5+
* 每轮游戏,我都会从 **1** 到 _**n**_ 随机选择一个数字。 请你猜选出的是哪个数字。
6+
* 如果你猜错了,我会告诉你,你猜测的数字比我选出的数字是大了还是小了。
7+
8+
你可以通过调用一个预先定义好的接口 `int guess(int num)` 来获取猜测结果,返回值一共有 3 种可能的情况(`-1``1` 或 `0`):
9+
10+
* -1:我选出的数字比你猜的数字小 `pick
11+
num`
12+
* 0:我选出的数字和你猜的数字一样。恭喜!你猜对了!`pick == num`
13+
14+
 
15+
16+
**示例 1:**
17+
18+
`
19+
**输入:**n = 10, pick = 6
20+
**输出:**6
21+
`
22+
23+
**示例 2:**
24+
25+
`
26+
**输入:**n = 1, pick = 1
27+
**输出:**1
28+
`
29+
30+
**示例 3:**
31+
32+
`
33+
**输入:**n = 2, pick = 1
34+
**输出:**1
35+
`
36+
37+
**示例 4:**
38+
39+
`
40+
**输入:**n = 2, pick = 2
41+
**输出:**2
42+
`
43+
44+
 
45+
46+
**提示:**
47+
48+
* `1
49+
31
50+
- 1`
51+
* `1 <= pick <= n`

questions/store.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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/简单/374/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-10-6",
23+
input1: 10,
24+
input2: 6,
25+
want: 6,
26+
},
27+
{
28+
name: "test-1-1",
29+
input1: 1,
30+
input2: 1,
31+
want: 1,
32+
},
33+
{
34+
name: "test-2-1",
35+
input1: 2,
36+
input2: 1,
37+
want: 1,
38+
},
39+
{
40+
name: "test-2-2",
41+
input1: 2,
42+
input2: 2,
43+
want: 2,
44+
},
45+
}
46+
47+
testLog := leet.NewTestLog(len(tests))
48+
defer testLog.Render()
49+
50+
timeoutDuration := time.Second * 2
51+
52+
for idx, test := range tests {
53+
// 超时检测
54+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
55+
solution.Export(test.input1, test.input2)
56+
cancel()
57+
})
58+
59+
if timeout {
60+
testLog.Fail(idx+1, test.name, "timeout")
61+
continue
62+
}
63+
64+
got := solution.Export(test.input1, test.input2)
65+
if !reflect.DeepEqual(test.want, got) {
66+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
67+
continue
68+
}
69+
70+
testLog.Pass(idx+1, test.name)
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package solution
2+
3+
var guessNum int
4+
5+
func Export(n, num int) int {
6+
guessNum = num
7+
return guess(n)
8+
}
9+
10+
func guess(num int) int {
11+
if num > guessNum {
12+
return -1
13+
} else if num < guessNum {
14+
return 1
15+
} else {
16+
return 0
17+
}
18+
}
19+
20+
/****************************************************/
21+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
22+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
23+
/****************************************************/
24+
25+
/**
26+
* Forward declaration of guess API.
27+
* @param num your guess
28+
* @return -1 if num is lower than the guess number
29+
* 1 if num is higher than the guess number
30+
* otherwise return 0
31+
* func guess(num int) int;
32+
*/
33+
34+
func guessNumber(n int) int {
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package solution
2+
3+
var guessNum int
4+
5+
func Export(n, num int) int {
6+
guessNum = num
7+
return guess(n)
8+
}
9+
10+
func guess(num int) int {
11+
if num > guessNum {
12+
return -1
13+
} else if num < guessNum {
14+
return 1
15+
} else {
16+
return 0
17+
}
18+
}
19+
20+
/****************************************************/
21+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
22+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
23+
/****************************************************/
24+
25+
/**
26+
* Forward declaration of guess API.
27+
* @param num your guess
28+
* @return -1 if num is lower than the guess number
29+
* 1 if num is higher than the guess number
30+
* otherwise return 0
31+
* func guess(num int) int;
32+
*/
33+
34+
func guessNumber(n int) int {
35+
36+
}

0 commit comments

Comments
 (0)