auto commit
This commit is contained in:
parent
061b375529
commit
6027156d73
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
## 题目链接
|
## 题目链接
|
||||||
|
|
||||||
[牛客网](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)
|
[牛客网](https://www.nowcoder.com/practice/6fe361ede7e54db1b84adc81d09d8524?tpId=13&tqId=11203&tab=answerKey&from=cyc_github)
|
||||||
|
|
||||||
## 题目描述
|
## 题目描述
|
||||||
|
|
||||||
@ -28,19 +28,17 @@ Output:
|
|||||||
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public boolean duplicate(int[] nums, int length, int[] duplication) {
|
public int duplicate(int[] nums) {
|
||||||
if (nums == null || length <= 0)
|
for (int i = 0; i < nums.length; i++) {
|
||||||
return false;
|
|
||||||
for (int i = 0; i < length; i++) {
|
|
||||||
while (nums[i] != i) {
|
while (nums[i] != i) {
|
||||||
if (nums[i] == nums[nums[i]]) {
|
if (nums[i] == nums[nums[i]]) {
|
||||||
duplication[0] = nums[i];
|
return nums[i];
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
swap(nums, i, nums[i]);
|
swap(nums, i, nums[i]);
|
||||||
}
|
}
|
||||||
|
swap(nums, i, nums[i]);
|
||||||
}
|
}
|
||||||
return false;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void swap(int[] nums, int i, int j) {
|
private void swap(int[] nums, int i, int j) {
|
||||||
@ -48,4 +46,6 @@ private void swap(int[] nums, int i, int j) {
|
|||||||
nums[i] = nums[j];
|
nums[i] = nums[j];
|
||||||
nums[j] = t;
|
nums[j] = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user