Skip to content

Commit 6533dd3

Browse files
committed
feat: add solutions to lc problem: No.0374
No.0374.Guess Number Higher or Lower
1 parent 49fcfcc commit 6533dd3

File tree

7 files changed

+100
-52
lines changed

7 files changed

+100
-52
lines changed

solution/0300-0399/0371.Sum of Two Integers/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
<!-- 这里可写通用的实现逻辑 -->
3838

39+
**方法一:位运算**
40+
3941
两数字的二进制形式 a,b ,求和 s = a + b ,a(i)、b(i) 分别表示 a、b 的第 i 个二进制位。一共有 4 种情况:
4042

4143
| a(i) | b(i) | 不进位的和 | 进位 |
@@ -52,6 +54,8 @@
5254
- 问题转换为求:“不进位的数 + 进位” 之和;
5355
- 循环,直至进位为 0,返回不进位的数即可(也可以用递归实现)。
5456

57+
时间复杂度 $O(\log n)$。
58+
5559
<!-- tabs:start -->
5660

5761
### **Python3**

solution/0300-0399/0374.Guess Number Higher or Lower/README.md

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@
6666

6767
<!-- 这里可写通用的实现逻辑 -->
6868

69+
**方法一:二分查找**
70+
71+
我们在区间 $[1,..n]$ 进行二分查找,找到第一个满足 `guess(x) <= 0` 的数,即为答案。
72+
73+
时间复杂度 $O(\log n)$。其中 $n$ 为题目给定的上限。
74+
6975
<!-- tabs:start -->
7076

7177
### **Python3**
@@ -91,6 +97,19 @@ class Solution:
9197
return left
9298
```
9399

100+
```python
101+
# The guess API is already defined for you.
102+
# @param num, your guess
103+
# @return -1 if num is higher than the picked number
104+
# 1 if num is lower than the picked number
105+
# otherwise return 0
106+
# def guess(num: int) -> int:
107+
108+
class Solution:
109+
def guessNumber(self, n: int) -> int:
110+
return bisect.bisect(range(1, n + 1), 0, key=lambda x: -guess(x))
111+
```
112+
94113
### **Java**
95114

96115
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -176,30 +195,44 @@ func guessNumber(n int) int {
176195
}
177196
```
178197

198+
```go
199+
/**
200+
* Forward declaration of guess API.
201+
* @param num your guess
202+
* @return -1 if num is higher than the picked number
203+
* 1 if num is lower than the picked number
204+
* otherwise return 0
205+
* func guess(num int) int;
206+
*/
207+
208+
func guessNumber(n int) int {
209+
return sort.Search(n, func(i int) bool {
210+
i++
211+
return guess(i) <= 0
212+
}) + 1
213+
}
214+
```
215+
179216
### **C#**
180217

181218
```cs
182-
/**
219+
/**
183220
* Forward declaration of guess API.
184221
* @param num your guess
185-
* @return -1 if num is lower than the guess number
186-
* 1 if num is higher than the guess number
222+
* @return -1 if num is higher than the picked number
223+
* 1 if num is lower than the picked number
187224
* otherwise return 0
188225
* int guess(int num);
189226
*/
190227

191228
public class Solution : GuessGame {
192229
public int GuessNumber(int n) {
193230
int left = 1, right = n;
194-
while (left < right)
195-
{
231+
while (left < right) {
196232
int mid = left + ((right - left) >> 1);
197-
if (guess(mid) <= 0)
198-
{
233+
if (guess(mid) <= 0) {
199234
right = mid;
200-
}
201-
else
202-
{
235+
} else {
203236
left = mid + 1;
204237
}
205238
}

solution/0300-0399/0374.Guess Number Higher or Lower/README_EN.md

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ class Solution:
7575
return left
7676
```
7777

78+
```python
79+
# The guess API is already defined for you.
80+
# @param num, your guess
81+
# @return -1 if num is higher than the picked number
82+
# 1 if num is lower than the picked number
83+
# otherwise return 0
84+
# def guess(num: int) -> int:
85+
86+
class Solution:
87+
def guessNumber(self, n: int) -> int:
88+
return bisect.bisect(range(1, n + 1), 0, key=lambda x: -guess(x))
89+
```
90+
7891
### **Java**
7992

8093
```java
@@ -158,30 +171,44 @@ func guessNumber(n int) int {
158171
}
159172
```
160173

174+
```go
175+
/**
176+
* Forward declaration of guess API.
177+
* @param num your guess
178+
* @return -1 if num is higher than the picked number
179+
* 1 if num is lower than the picked number
180+
* otherwise return 0
181+
* func guess(num int) int;
182+
*/
183+
184+
func guessNumber(n int) int {
185+
return sort.Search(n, func(i int) bool {
186+
i++
187+
return guess(i) <= 0
188+
}) + 1
189+
}
190+
```
191+
161192
### **C#**
162193

163194
```cs
164-
/**
195+
/**
165196
* Forward declaration of guess API.
166197
* @param num your guess
167-
* @return -1 if num is lower than the guess number
168-
* 1 if num is higher than the guess number
198+
* @return -1 if num is higher than the picked number
199+
* 1 if num is lower than the picked number
169200
* otherwise return 0
170201
* int guess(int num);
171202
*/
172203

173204
public class Solution : GuessGame {
174205
public int GuessNumber(int n) {
175206
int left = 1, right = n;
176-
while (left < right)
177-
{
207+
while (left < right) {
178208
int mid = left + ((right - left) >> 1);
179-
if (guess(mid) <= 0)
180-
{
209+
if (guess(mid) <= 0) {
181210
right = mid;
182-
}
183-
else
184-
{
211+
} else {
185212
left = mid + 1;
186213
}
187214
}

solution/0300-0399/0374.Guess Number Higher or Lower/Solution.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
/**
22
* Forward declaration of guess API.
33
* @param num your guess
4-
* @return -1 if num is lower than the guess number
5-
* 1 if num is higher than the guess number
4+
* @return -1 if num is higher than the picked number
5+
* 1 if num is lower than the picked number
66
* otherwise return 0
77
* int guess(int num);
88
*/
99

1010
public class Solution : GuessGame {
1111
public int GuessNumber(int n) {
1212
int left = 1, right = n;
13-
while (left < right)
14-
{
13+
while (left < right) {
1514
int mid = left + ((right - left) >> 1);
16-
if (guess(mid) <= 0)
17-
{
18-
right = mid;
19-
}
20-
else
21-
{
15+
if (guess(mid) <= 0) {
16+
right = mid;
17+
} else {
2218
left = mid + 1;
2319
}
2420
}
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
/**
22
* Forward declaration of guess API.
33
* @param num your guess
4-
* @return -1 if num is lower than the guess number
5-
* 1 if num is higher than the guess number
4+
* @return -1 if num is higher than the picked number
5+
* 1 if num is lower than the picked number
66
* otherwise return 0
77
* func guess(num int) int;
88
*/
99

1010
func guessNumber(n int) int {
11-
left, right := 1, n
12-
for left < right {
13-
mid := (left + right) >> 1
14-
if guess(mid) <= 0 {
15-
right = mid
16-
} else {
17-
left = mid + 1
18-
}
19-
}
20-
return left
11+
return sort.Search(n, func(i int) bool {
12+
i++
13+
return guess(i) <= 0
14+
}) + 1
2115
}
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
# The guess API is already defined for you.
22
# @param num, your guess
3-
# @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
3+
# @return -1 if num is higher than the picked number
4+
# 1 if num is lower than the picked number
5+
# otherwise return 0
46
# def guess(num: int) -> int:
57

68

79
class Solution:
810
def guessNumber(self, n: int) -> int:
9-
left, right = 1, n
10-
while left < right:
11-
mid = (left + right) >> 1
12-
if guess(mid) <= 0:
13-
right = mid
14-
else:
15-
left = mid + 1
16-
return left
11+
return bisect.bisect(range(1, n + 1), 0, key=lambda x: -guess(x))

solution/1400-1499/1429.First Unique Number/Solution.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class FirstUnique:
2-
32
def __init__(self, nums: List[int]):
43
self.cnt = Counter(nums)
54
self.q = deque(nums)

0 commit comments

Comments
 (0)