diff --git a/notes/3. 数组中重复的数字.md b/notes/3. 数组中重复的数字.md index 6b9366fd..babab09d 100644 --- a/notes/3. 数组中重复的数字.md +++ b/notes/3. 数组中重复的数字.md @@ -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 -public boolean duplicate(int[] nums, int length, int[] duplication) { - if (nums == null || length <= 0) - return false; - for (int i = 0; i < length; i++) { +public int duplicate(int[] nums) { + for (int i = 0; i < nums.length; i++) { while (nums[i] != i) { if (nums[i] == nums[nums[i]]) { - duplication[0] = nums[i]; - return true; + return 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) { @@ -48,4 +46,6 @@ private void swap(int[] nums, int i, int j) { nums[i] = nums[j]; nums[j] = t; } + ``` +