From 6027156d7304106369cd367cc007913c1a9b7f74 Mon Sep 17 00:00:00 2001 From: CyC2018 Date: Fri, 19 Mar 2021 03:15:18 +0800 Subject: [PATCH] auto commit --- notes/3. 数组中重复的数字.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) 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; } + ``` +