File tree 1 file changed +8
-8
lines changed
1 file changed +8
-8
lines changed Original file line number Diff line number Diff line change 2
2
3
3
## 题目链接
4
4
5
- [ 牛客网] ( https://www.nowcoder.com/practice/623a5ac0ea5b4e5f95552655361ae0a8 ?tpId=13&tqId=11203&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking &from=cyc_github )
5
+ [ 牛客网] ( https://www.nowcoder.com/practice/6fe361ede7e54db1b84adc81d09d8524 ?tpId=13&tqId=11203&tab=answerKey &from=cyc_github )
6
6
7
7
## 题目描述
8
8
@@ -28,24 +28,24 @@ Output:
28
28
29
29
30
30
``` java
31
- public boolean duplicate(int [] nums, int length, int [] duplication) {
32
- if (nums == null || length <= 0 )
33
- return false ;
34
- for (int i = 0 ; i < length; i++ ) {
31
+ public int duplicate(int [] nums) {
32
+ for (int i = 0 ; i < nums. length; i++ ) {
35
33
while (nums[i] != i) {
36
34
if (nums[i] == nums[nums[i]]) {
37
- duplication[0 ] = nums[i];
38
- return true ;
35
+ return nums[i];
39
36
}
40
37
swap(nums, i, nums[i]);
41
38
}
39
+ swap(nums, i, nums[i]);
42
40
}
43
- return false ;
41
+ return - 1 ;
44
42
}
45
43
46
44
private void swap(int [] nums, int i, int j) {
47
45
int t = nums[i];
48
46
nums[i] = nums[j];
49
47
nums[j] = t;
50
48
}
49
+
51
50
```
51
+
You can’t perform that action at this time.
0 commit comments