Skip to content

Commit b368be2

Browse files
committed
feat: update solutions to lcof problem: No.0053
1 parent ada70a6 commit b368be2

File tree

6 files changed

+201
-160
lines changed

6 files changed

+201
-160
lines changed

lcof/面试题53 - I. 在排序数组中查找数字 I/README.md

+111-89
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,34 @@
2424

2525
## 解法
2626

27+
两遍二分,分别查找出左边界和右边界。
28+
2729
<!-- tabs:start -->
2830

2931
### **Python3**
3032

3133
```python
3234
class Solution:
3335
def search(self, nums: List[int], target: int) -> int:
34-
if not nums:
36+
if len(nums) == 0:
37+
return 0
38+
left, right = 0, len(nums) - 1
39+
while left < right:
40+
mid = (left + right) >> 1
41+
if nums[mid] >= target:
42+
right = mid
43+
else:
44+
left = mid + 1
45+
if nums[left] != target:
3546
return 0
36-
l, r = 0, len(nums) - 1
37-
while l <= r:
38-
m = (l + r) >> 1
39-
if nums[m] == target:
40-
return self._count(nums, l, r, m)
41-
if nums[m] < target:
42-
l = m + 1
47+
l, right = left, len(nums) - 1
48+
while left < right:
49+
mid = (left + right + 1) >> 1
50+
if nums[mid] <= target:
51+
left = mid
4352
else:
44-
r = m - 1
45-
return 0
46-
47-
def _count(self, nums, l, r, m) -> int:
48-
cnt = 0
49-
for i in range(m, l - 1, -1):
50-
if nums[i] == nums[m]:
51-
cnt += 1
52-
elif nums[i] < nums[m]:
53-
break
54-
55-
for i in range(m + 1, r + 1):
56-
if nums[i] == nums[m]:
57-
cnt += 1
58-
elif nums[i] > nums[m]:
59-
break
60-
return cnt
53+
right = mid - 1
54+
return left - l + 1
6155
```
6256

6357
### **Java**
@@ -68,82 +62,38 @@ class Solution {
6862
if (nums.length == 0) {
6963
return 0;
7064
}
71-
int l = 0, r = nums.length - 1;
72-
while (l <= r) {
73-
int m = (l + r) >>> 1;
74-
if (nums[m] == target) {
75-
return count(nums, l, r, m);
76-
}
77-
if (nums[m] < target) {
78-
l = m + 1;
65+
// find first position
66+
int left = 0, right = nums.length - 1;
67+
while (left < right) {
68+
int mid = (left + right) >>> 1;
69+
if (nums[mid] >= target) {
70+
right = mid;
7971
} else {
80-
r = m - 1;
72+
left = mid + 1;
8173
}
8274
}
83-
return 0;
84-
}
85-
86-
private int count(int[] nums, int l, int r, int m) {
87-
int cnt = 0;
88-
for (int i = m; i >= l; --i) {
89-
if (nums[i] == nums[m]) {
90-
++cnt;
91-
} else if (nums[i] < nums[m]) {
92-
break;
93-
}
75+
if (nums[left] != target) {
76+
return 0;
9477
}
95-
for (int i = m + 1; i <= r; ++i) {
96-
if (nums[i] == nums[m]) {
97-
++cnt;
98-
} else if (nums[i] > nums[m]) {
99-
break;
78+
int l = left;
79+
80+
// find last position
81+
right = nums.length - 1;
82+
while (left < right) {
83+
int mid = (left + right + 1) >>> 1;
84+
if (nums[mid] <= target) {
85+
left = mid;
86+
} else {
87+
right = mid - 1;
10088
}
10189
}
102-
return cnt;
90+
return left - l + 1;
10391
}
10492
}
10593
```
10694

107-
### **JavaScript**
108-
109-
```js
110-
/**
111-
* @param {number[]} nums
112-
* @param {number} target
113-
* @return {number}
114-
*/
115-
var search = function (nums, target) {
116-
if (!nums || !nums.length) return 0;
117-
let left = 0;
118-
let right = nums.length - 1;
119-
let res = 0;
120-
while (left < right) {
121-
let mid = left + ~~((right - left) / 2);
122-
if (nums[mid] < target) {
123-
left = mid + 1;
124-
} else if (nums[mid] > target) {
125-
right = mid;
126-
} else {
127-
left = mid;
128-
right = mid;
129-
break;
130-
}
131-
}
132-
while (nums[left] === target) {
133-
res++;
134-
left--;
135-
}
136-
while (nums[++right] === target) {
137-
res++;
138-
}
139-
return res;
140-
};
141-
```
142-
14395
### **C++**
14496

145-
两遍二分,分别查找出左边界和右边界。
146-
14797
```cpp
14898
class Solution {
14999
public:
@@ -178,6 +128,78 @@ public:
178128
};
179129
```
180130
131+
### **Go**
132+
133+
```go
134+
func search(nums []int, target int) int {
135+
if len(nums) == 0 {
136+
return 0
137+
}
138+
left, right := 0, len(nums)-1
139+
for left < right {
140+
mid := (left + right) >> 1
141+
if nums[mid] >= target {
142+
right = mid
143+
} else {
144+
left = mid + 1
145+
}
146+
}
147+
if nums[left] != target {
148+
return 0
149+
}
150+
l := left
151+
right = len(nums) - 1
152+
for left < right {
153+
mid := (left + right + 1) >> 1
154+
if nums[mid] <= target {
155+
left = mid
156+
} else {
157+
right = mid - 1
158+
}
159+
}
160+
return left - l + 1
161+
}
162+
```
163+
164+
### **JavaScript**
165+
166+
```js
167+
/**
168+
* @param {number[]} nums
169+
* @param {number} target
170+
* @return {number}
171+
*/
172+
var search = function(nums, target) {
173+
if (nums.length == 0) {
174+
return 0;
175+
}
176+
let left = 0;
177+
let right = nums.length - 1;
178+
while (left < right) {
179+
const mid = (left + right) >> 1;
180+
if (nums[mid] >= target) {
181+
right = mid;
182+
} else {
183+
left = mid + 1;
184+
}
185+
}
186+
if (nums[left] != target) {
187+
return 0;
188+
}
189+
let l = left;
190+
right = nums.length - 1;
191+
while (left < right) {
192+
const mid = (left + right + 1) >> 1;
193+
if (nums[mid] <= target) {
194+
left = mid;
195+
} else {
196+
right = mid - 1;
197+
}
198+
}
199+
return left - l + 1;
200+
};
201+
```
202+
181203
### **...**
182204

183205
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
func search(nums []int, target int) int {
2+
if len(nums) == 0 {
3+
return 0
4+
}
5+
left, right := 0, len(nums)-1
6+
for left < right {
7+
mid := (left + right) >> 1
8+
if nums[mid] >= target {
9+
right = mid
10+
} else {
11+
left = mid + 1
12+
}
13+
}
14+
if nums[left] != target {
15+
return 0
16+
}
17+
l := left
18+
right = len(nums) - 1
19+
for left < right {
20+
mid := (left + right + 1) >> 1
21+
if nums[mid] <= target {
22+
left = mid
23+
} else {
24+
right = mid - 1
25+
}
26+
}
27+
return left - l + 1
28+
}

lcof/面试题53 - I. 在排序数组中查找数字 I/Solution.java

+20-26
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,31 @@ public int search(int[] nums, int target) {
33
if (nums.length == 0) {
44
return 0;
55
}
6-
int l = 0, r = nums.length - 1;
7-
while (l <= r) {
8-
int m = (l + r) >>> 1;
9-
if (nums[m] == target) {
10-
return count(nums, l, r, m);
11-
}
12-
if (nums[m] < target) {
13-
l = m + 1;
6+
// find first position
7+
int left = 0, right = nums.length - 1;
8+
while (left < right) {
9+
int mid = (left + right) >>> 1;
10+
if (nums[mid] >= target) {
11+
right = mid;
1412
} else {
15-
r = m - 1;
13+
left = mid + 1;
1614
}
1715
}
18-
return 0;
19-
}
20-
21-
private int count(int[] nums, int l, int r, int m) {
22-
int cnt = 0;
23-
for (int i = m; i >= l; --i) {
24-
if (nums[i] == nums[m]) {
25-
++cnt;
26-
} else if (nums[i] < nums[m]) {
27-
break;
28-
}
16+
if (nums[left] != target) {
17+
return 0;
2918
}
30-
for (int i = m + 1; i <= r; ++i) {
31-
if (nums[i] == nums[m]) {
32-
++cnt;
33-
} else if (nums[i] > nums[m]) {
34-
break;
19+
int l = left;
20+
21+
// find last position
22+
right = nums.length - 1;
23+
while (left < right) {
24+
int mid = (left + right + 1) >>> 1;
25+
if (nums[mid] <= target) {
26+
left = mid;
27+
} else {
28+
right = mid - 1;
3529
}
3630
}
37-
return cnt;
31+
return left - l + 1;
3832
}
3933
}

lcof/面试题53 - I. 在排序数组中查找数字 I/Solution.js

+23-20
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,32 @@
33
* @param {number} target
44
* @return {number}
55
*/
6-
var search = function (nums, target) {
7-
if (!nums || !nums.length) return 0;
6+
var search = function(nums, target) {
7+
if (nums.length == 0) {
8+
return 0;
9+
}
810
let left = 0;
911
let right = nums.length - 1;
10-
let res = 0;
1112
while (left < right) {
12-
let mid = left + ~~((right - left) / 2);
13-
if (nums[mid] < target) {
14-
left = mid + 1;
15-
} else if (nums[mid] > target) {
16-
right = mid;
17-
} else {
18-
left = mid;
19-
right = mid;
20-
break;
21-
}
13+
const mid = (left + right) >> 1;
14+
if (nums[mid] >= target) {
15+
right = mid;
16+
} else {
17+
left = mid + 1;
18+
}
2219
}
23-
while (nums[left] === target) {
24-
res++;
25-
left--;
20+
if (nums[left] != target) {
21+
return 0;
2622
}
27-
while (nums[++right] === target) {
28-
res++;
23+
let l = left;
24+
right = nums.length - 1;
25+
while (left < right) {
26+
const mid = (left + right + 1) >> 1;
27+
if (nums[mid] <= target) {
28+
left = mid;
29+
} else {
30+
right = mid - 1;
31+
}
2932
}
30-
return res;
31-
};
33+
return left - l + 1;
34+
};

0 commit comments

Comments
 (0)