剑指 Offer 03. 数组中重复的数字 发表于 2022-01-25 1234567891011121314151617181920212223class Solution { public int findRepeatNumber(int[] nums) { for (int i = 0; i < nums.length; i++) { int targetIndex = nums[i]; while (nums[i] != nums[targetIndex]) { swap(nums, i, targetIndex); targetIndex = nums[i]; } if (i != targetIndex) { return nums[i]; } } throw new RuntimeException("Can't find repeat number"); } private void swap(int[] nums, int i, int j) { int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; }} Reference剑指 Offer 03. 数组中重复的数字