Skip to content

Commit 4f82fc8

Browse files
authored
feat: update solutions to lc problem: No.0374 (doocs#2985)
1 parent 05c535b commit 4f82fc8

File tree

10 files changed

+62
-208
lines changed

10 files changed

+62
-208
lines changed

lcof2/剑指 Offer II 002. 二进制加法/README.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,14 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcof2/%E5%89%91%E6%8C%87%2
6262
```python
6363
class Solution:
6464
def addBinary(self, a: str, b: str) -> str:
65-
return bin(int(a, 2) + int(b, 2))[2:]
65+
ans = []
66+
i, j, carry = len(a) - 1, len(b) - 1, 0
67+
while i >= 0 or j >= 0 or carry:
68+
carry += (0 if i < 0 else int(a[i])) + (0 if j < 0 else int(b[j]))
69+
carry, v = divmod(carry, 2)
70+
ans.append(str(v))
71+
i, j = i - 1, j - 1
72+
return ''.join(ans[::-1])
6673
```
6774

6875
#### Java
@@ -128,7 +135,16 @@ func addBinary(a string, b string) string {
128135

129136
```ts
130137
function addBinary(a: string, b: string): string {
131-
return (BigInt('0b' + a) + BigInt('0b' + b)).toString(2);
138+
let i = a.length - 1;
139+
let j = b.length - 1;
140+
let ans: number[] = [];
141+
for (let carry = 0; i >= 0 || j >= 0 || carry; --i, --j) {
142+
carry += (i >= 0 ? a[i] : '0').charCodeAt(0) - '0'.charCodeAt(0);
143+
carry += (j >= 0 ? b[j] : '0').charCodeAt(0) - '0'.charCodeAt(0);
144+
ans.push(carry % 2);
145+
carry >>= 1;
146+
}
147+
return ans.reverse().join('');
132148
}
133149
```
134150

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
class Solution:
22
def addBinary(self, a: str, b: str) -> str:
3-
return bin(int(a, 2) + int(b, 2))[2:]
3+
ans = []
4+
i, j, carry = len(a) - 1, len(b) - 1, 0
5+
while i >= 0 or j >= 0 or carry:
6+
carry += (0 if i < 0 else int(a[i])) + (0 if j < 0 else int(b[j]))
7+
carry, v = divmod(carry, 2)
8+
ans.append(str(v))
9+
i, j = i - 1, j - 1
10+
return "".join(ans[::-1])

lcof2/剑指 Offer II 002. 二进制加法/Solution2.py

-10
This file was deleted.

lcof2/剑指 Offer II 002. 二进制加法/Solution2.ts

-12
This file was deleted.

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

+11-68
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ tags:
7575

7676
我们在区间 $[1,..n]$ 进行二分查找,找到第一个满足 `guess(x) <= 0` 的数,即为答案。
7777

78-
时间复杂度 $O(\log n)$。其中 $n$ 为题目给定的上限。
78+
时间复杂度 $O(\log n)$。其中 $n$ 为题目给定的上限。空间复杂度 $O(1)$。
7979

8080
<!-- tabs:start -->
8181

@@ -84,20 +84,15 @@ tags:
8484
```python
8585
# The guess API is already defined for you.
8686
# @param num, your guess
87-
# @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
87+
# @return -1 if num is higher than the picked number
88+
# 1 if num is lower than the picked number
89+
# otherwise return 0
8890
# def guess(num: int) -> int:
8991

9092

9193
class Solution:
9294
def guessNumber(self, n: int) -> int:
93-
left, right = 1, n
94-
while left < right:
95-
mid = (left + right) >> 1
96-
if guess(mid) <= 0:
97-
right = mid
98-
else:
99-
left = mid + 1
100-
return left
95+
return bisect.bisect(range(1, n + 1), 0, key=lambda x: -guess(x))
10196
```
10297

10398
#### Java
@@ -163,23 +158,17 @@ public:
163158
/**
164159
* Forward declaration of guess API.
165160
* @param num your guess
166-
* @return -1 if num is lower than the guess number
167-
* 1 if num is higher than the guess number
161+
* @return -1 if num is higher than the picked number
162+
* 1 if num is lower than the picked number
168163
* otherwise return 0
169164
* func guess(num int) int;
170165
*/
171166
172167
func guessNumber(n int) int {
173-
left, right := 1, n
174-
for left < right {
175-
mid := (left + right) >> 1
176-
if guess(mid) <= 0 {
177-
right = mid
178-
} else {
179-
left = mid + 1
180-
}
181-
}
182-
return left
168+
return sort.Search(n, func(i int) bool {
169+
i++
170+
return guess(i) <= 0
171+
}) + 1
183172
}
184173
```
185174

@@ -277,50 +266,4 @@ public class Solution : GuessGame {
277266

278267
<!-- solution:end -->
279268

280-
<!-- solution:start -->
281-
282-
### 方法二
283-
284-
<!-- tabs:start -->
285-
286-
#### Python3
287-
288-
```python
289-
# The guess API is already defined for you.
290-
# @param num, your guess
291-
# @return -1 if num is higher than the picked number
292-
# 1 if num is lower than the picked number
293-
# otherwise return 0
294-
# def guess(num: int) -> int:
295-
296-
297-
class Solution:
298-
def guessNumber(self, n: int) -> int:
299-
return bisect.bisect(range(1, n + 1), 0, key=lambda x: -guess(x))
300-
```
301-
302-
#### Go
303-
304-
```go
305-
/**
306-
* Forward declaration of guess API.
307-
* @param num your guess
308-
* @return -1 if num is higher than the picked number
309-
* 1 if num is lower than the picked number
310-
* otherwise return 0
311-
* func guess(num int) int;
312-
*/
313-
314-
func guessNumber(n int) int {
315-
return sort.Search(n, func(i int) bool {
316-
i++
317-
return guess(i) <= 0
318-
}) + 1
319-
}
320-
```
321-
322-
<!-- tabs:end -->
323-
324-
<!-- solution:end -->
325-
326269
<!-- problem:end -->

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

+15-68
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ tags:
6969

7070
<!-- solution:start -->
7171

72-
### Solution 1
72+
### Solution 1: Binary Search
73+
74+
We perform a binary search in the interval $[1,..n]$, and find the first number that satisfies `guess(x) <= 0`, which is the answer.
75+
76+
The time complexity is $O(\log n)$, where $n$ is the upper limit given in the problem. The space complexity is $O(1)$.
7377

7478
<!-- tabs:start -->
7579

@@ -78,20 +82,15 @@ tags:
7882
```python
7983
# The guess API is already defined for you.
8084
# @param num, your guess
81-
# @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
85+
# @return -1 if num is higher than the picked number
86+
# 1 if num is lower than the picked number
87+
# otherwise return 0
8288
# def guess(num: int) -> int:
8389

8490

8591
class Solution:
8692
def guessNumber(self, n: int) -> int:
87-
left, right = 1, n
88-
while left < right:
89-
mid = (left + right) >> 1
90-
if guess(mid) <= 0:
91-
right = mid
92-
else:
93-
left = mid + 1
94-
return left
93+
return bisect.bisect(range(1, n + 1), 0, key=lambda x: -guess(x))
9594
```
9695

9796
#### Java
@@ -157,23 +156,17 @@ public:
157156
/**
158157
* Forward declaration of guess API.
159158
* @param num your guess
160-
* @return -1 if num is lower than the guess number
161-
* 1 if num is higher than the guess number
159+
* @return -1 if num is higher than the picked number
160+
* 1 if num is lower than the picked number
162161
* otherwise return 0
163162
* func guess(num int) int;
164163
*/
165164
166165
func guessNumber(n int) int {
167-
left, right := 1, n
168-
for left < right {
169-
mid := (left + right) >> 1
170-
if guess(mid) <= 0 {
171-
right = mid
172-
} else {
173-
left = mid + 1
174-
}
175-
}
176-
return left
166+
return sort.Search(n, func(i int) bool {
167+
i++
168+
return guess(i) <= 0
169+
}) + 1
177170
}
178171
```
179172

@@ -270,50 +263,4 @@ public class Solution : GuessGame {
270263

271264
<!-- solution:end -->
272265

273-
<!-- solution:start -->
274-
275-
### Solution 2
276-
277-
<!-- tabs:start -->
278-
279-
#### Python3
280-
281-
```python
282-
# The guess API is already defined for you.
283-
# @param num, your guess
284-
# @return -1 if num is higher than the picked number
285-
# 1 if num is lower than the picked number
286-
# otherwise return 0
287-
# def guess(num: int) -> int:
288-
289-
290-
class Solution:
291-
def guessNumber(self, n: int) -> int:
292-
return bisect.bisect(range(1, n + 1), 0, key=lambda x: -guess(x))
293-
```
294-
295-
#### Go
296-
297-
```go
298-
/**
299-
* Forward declaration of guess API.
300-
* @param num your guess
301-
* @return -1 if num is higher than the picked number
302-
* 1 if num is lower than the picked number
303-
* otherwise return 0
304-
* func guess(num int) int;
305-
*/
306-
307-
func guessNumber(n int) int {
308-
return sort.Search(n, func(i int) bool {
309-
i++
310-
return guess(i) <= 0
311-
}) + 1
312-
}
313-
```
314-
315-
<!-- tabs:end -->
316-
317-
<!-- solution:end -->
318-
319266
<!-- problem:end -->
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
}
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/0300-0399/0374.Guess Number Higher or Lower/Solution2.go

-15
This file was deleted.

solution/0300-0399/0374.Guess Number Higher or Lower/Solution2.py

-11
This file was deleted.

0 commit comments

Comments
 (0)