Skip to content

Commit 9c3f572

Browse files
committed
feat: update solutions to leetcode problem: No.0704
1 parent 45e286b commit 9c3f572

File tree

4 files changed

+57
-24
lines changed

4 files changed

+57
-24
lines changed

solution/0700-0799/0704.Binary Search/README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,23 @@
5151
<!-- 这里可写当前语言的特殊实现逻辑 -->
5252

5353
```java
54-
54+
class Solution {
55+
public int search(int[] nums, int target) {
56+
int low = 0, high = nums.length - 1;
57+
while (low <= high) {
58+
int mid = low + ((high - low) >> 1);
59+
if (nums[mid] == target) {
60+
return mid;
61+
}
62+
if (nums[mid] < target) {
63+
low = mid + 1;
64+
} else {
65+
high = mid - 1;
66+
}
67+
}
68+
return -1;
69+
}
70+
}
5571
```
5672

5773
### **...**

solution/0700-0799/0704.Binary Search/README_EN.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,40 @@
5151
### **Python3**
5252

5353
```python
54-
54+
class Solution:
55+
def search(self, nums: List[int], target: int) -> int:
56+
low, high = 0, len(nums) - 1
57+
while low <= high:
58+
mid = low + ((high - low) >> 1)
59+
if nums[mid] == target:
60+
return mid
61+
if nums[mid] < target:
62+
low = mid + 1
63+
else:
64+
high = mid - 1
65+
return -1
5566
```
5667

5768
### **Java**
5869

5970
```java
60-
71+
class Solution {
72+
public int search(int[] nums, int target) {
73+
int low = 0, high = nums.length - 1;
74+
while (low <= high) {
75+
int mid = low + ((high - low) >> 1);
76+
if (nums[mid] == target) {
77+
return mid;
78+
}
79+
if (nums[mid] < target) {
80+
low = mid + 1;
81+
} else {
82+
high = mid - 1;
83+
}
84+
}
85+
return -1;
86+
}
87+
}
6188
```
6289

6390
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
class Solution {
22
public int search(int[] nums, int target) {
3-
int l = 0, r = nums.length - 1;
4-
while (l <= r) {
5-
int mid = l + r >>> 1;
3+
int low = 0, high = nums.length - 1;
4+
while (low <= high) {
5+
int mid = low + ((high - low) >> 1);
6+
if (nums[mid] == target) {
7+
return mid;
8+
}
69
if (nums[mid] < target) {
7-
l = mid + 1;
8-
} else if (nums[mid] > target) {
9-
r = mid - 1;
10+
low = mid + 1;
1011
} else {
11-
return mid;
12+
high = mid - 1;
1213
}
1314
}
1415
return -1;
1516
}
16-
}
17+
}

solution/0700-0799/0704.Binary Search/Solution.py

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
1-
'''
2-
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise return -1.
3-
'''
4-
5-
# Perfomance
6-
'''
7-
Runtime: 268 ms, faster than 84.25% of Python3 online submissions for Binary Search.
8-
Memory Usage: 14 MB, less than 100.00% of Python3 online submissions for Binary Search.
9-
'''
10-
11-
121
class Solution:
132
def search(self, nums: List[int], target: int) -> int:
143
low, high = 0, len(nums) - 1
154
while low <= high:
16-
mid = low + (high - low) // 2
5+
mid = low + ((high - low) >> 1)
176
if nums[mid] == target:
187
return mid
19-
elif nums[mid] < target:
8+
if nums[mid] < target:
209
low = mid + 1
2110
else:
2211
high = mid - 1

0 commit comments

Comments
 (0)