1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class 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; } }
|