Skip to content

Commit 75b824a

Browse files
committed
feat: update leetcode solutions: No.0287. Find the Duplicate Number
1 parent e2b096c commit 75b824a

File tree

5 files changed

+143
-17
lines changed

5 files changed

+143
-17
lines changed

solution/0200-0299/0287.Find the Duplicate Number/README.md

+53-1
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,74 @@
3232

3333
<!-- 这里可写通用的实现逻辑 -->
3434

35+
二分法。
36+
37+
如果值范围在 `[1, mid]` 的数小于等于 mid,说明此范围内没有重复的数,否则说明有重复数。
38+
3539
<!-- tabs:start -->
3640

3741
### **Python3**
3842

3943
<!-- 这里可写当前语言的特殊实现逻辑 -->
4044

4145
```python
42-
46+
class Solution:
47+
def findDuplicate(self, nums: List[int]) -> int:
48+
l, r = 0, len(nums) - 1
49+
while l < r:
50+
mid = (l + r) >> 1
51+
cnt = 0
52+
for e in nums:
53+
if e <= mid:
54+
cnt += 1
55+
if cnt <= mid:
56+
l = mid + 1
57+
else:
58+
r = mid
59+
return l
4360
```
4461

4562
### **Java**
4663

4764
<!-- 这里可写当前语言的特殊实现逻辑 -->
4865

4966
```java
67+
class Solution {
68+
public int findDuplicate(int[] nums) {
69+
int l = 1, r = nums.length - 1;
70+
while (l < r) {
71+
int mid = (l + r) >>> 1;
72+
int cnt = 0;
73+
for (int e : nums) {
74+
if (e <= mid) ++cnt;
75+
}
76+
if (cnt <= mid) l = mid + 1;
77+
else r = mid;
78+
}
79+
return l;
80+
}
81+
}
82+
```
5083

84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
int findDuplicate(vector<int>& nums) {
90+
int l = 0, r = nums.size() - 1;
91+
while (l < r) {
92+
int mid = l + ((r - l) >> 1);
93+
int cnt = 0;
94+
for (auto e : nums) {
95+
if (e <= mid) ++cnt;
96+
}
97+
if (cnt <= mid) l = mid + 1;
98+
else r = mid;
99+
}
100+
return l;
101+
}
102+
};
51103
```
52104
53105
### **...**

solution/0200-0299/0287.Find the Duplicate Number/README_EN.md

+49-1
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,61 @@
4040
### **Python3**
4141

4242
```python
43-
43+
class Solution:
44+
def findDuplicate(self, nums: List[int]) -> int:
45+
l, r = 0, len(nums) - 1
46+
while l < r:
47+
mid = (l + r) >> 1
48+
cnt = 0
49+
for e in nums:
50+
if e <= mid:
51+
cnt += 1
52+
if cnt <= mid:
53+
l = mid + 1
54+
else:
55+
r = mid
56+
return l
4457
```
4558

4659
### **Java**
4760

4861
```java
62+
class Solution {
63+
public int findDuplicate(int[] nums) {
64+
int l = 1, r = nums.length - 1;
65+
while (l < r) {
66+
int mid = (l + r) >>> 1;
67+
int cnt = 0;
68+
for (int e : nums) {
69+
if (e <= mid) ++cnt;
70+
}
71+
if (cnt <= mid) l = mid + 1;
72+
else r = mid;
73+
}
74+
return l;
75+
}
76+
}
77+
```
4978

79+
### **C++**
80+
81+
```cpp
82+
class Solution {
83+
public:
84+
int findDuplicate(vector<int>& nums) {
85+
int l = 0, r = nums.size() - 1;
86+
while (l < r) {
87+
int mid = l + ((r - l) >> 1);
88+
int cnt = 0;
89+
for (auto e : nums) {
90+
if (e <= mid) ++cnt;
91+
}
92+
if (cnt <= mid) l = mid + 1;
93+
else r = mid;
94+
}
95+
return l;
96+
}
97+
};
5098
```
5199
52100
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int findDuplicate(vector<int>& nums) {
4+
int l = 0, r = nums.size() - 1;
5+
while (l < r) {
6+
int mid = l + ((r - l) >> 1);
7+
int cnt = 0;
8+
for (auto e : nums) {
9+
if (e <= mid) ++cnt;
10+
}
11+
if (cnt <= mid) l = mid + 1;
12+
else r = mid;
13+
}
14+
return l;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
public class Solution {
2-
// https://segmentfault.com/a/1190000003817671
1+
class Solution {
32
public int findDuplicate(int[] nums) {
4-
int slow = 0;
5-
int fast = 0;
6-
// 找到快慢指针相遇的地方
7-
do{
8-
slow = nums[slow];
9-
fast = nums[nums[fast]];
10-
} while(slow != fast);
11-
int find = 0;
12-
// 用一个新指针从头开始,直到和慢指针相遇
13-
while(find != slow){
14-
slow = nums[slow];
15-
find = nums[find];
3+
int l = 1, r = nums.length - 1;
4+
while (l < r) {
5+
int mid = (l + r) >>> 1;
6+
int cnt = 0;
7+
for (int e : nums) {
8+
if (e <= mid) ++cnt;
9+
}
10+
if (cnt <= mid) l = mid + 1;
11+
else r = mid;
1612
}
17-
return find;
13+
return l;
1814
}
1915
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def findDuplicate(self, nums: List[int]) -> int:
3+
l, r = 0, len(nums) - 1
4+
while l < r:
5+
mid = (l + r) >> 1
6+
cnt = 0
7+
for e in nums:
8+
if e <= mid:
9+
cnt += 1
10+
if cnt <= mid:
11+
l = mid + 1
12+
else:
13+
r = mid
14+
return l

0 commit comments

Comments
 (0)