File tree 5 files changed +143
-17
lines changed
solution/0200-0299/0287.Find the Duplicate Number
5 files changed +143
-17
lines changed Original file line number Diff line number Diff line change 32
32
33
33
<!-- 这里可写通用的实现逻辑 -->
34
34
35
+ 二分法。
36
+
37
+ 如果值范围在 ` [1, mid] ` 的数小于等于 mid,说明此范围内没有重复的数,否则说明有重复数。
38
+
35
39
<!-- tabs:start -->
36
40
37
41
### ** Python3**
38
42
39
43
<!-- 这里可写当前语言的特殊实现逻辑 -->
40
44
41
45
``` 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
43
60
```
44
61
45
62
### ** Java**
46
63
47
64
<!-- 这里可写当前语言的特殊实现逻辑 -->
48
65
49
66
``` 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
+ ```
50
83
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
+ };
51
103
```
52
104
53
105
### **...**
Original file line number Diff line number Diff line change 40
40
### ** Python3**
41
41
42
42
``` 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
44
57
```
45
58
46
59
### ** Java**
47
60
48
61
``` 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
+ ```
49
78
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
+ };
50
98
```
51
99
52
100
### **...**
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change 1
- public class Solution {
2
- // https://segmentfault.com/a/1190000003817671
1
+ class Solution {
3
2
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 ;
16
12
}
17
- return find ;
13
+ return l ;
18
14
}
19
15
}
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments