1、前言
题目描述2、思路
思路跟 leetcode 442 题一样。
3、代码
class Solution {
public int findRepeatNumber (int[] numbers) {
if(numbers == null || numbers.length == 0){
return -1;
}
for(int i = 0; i < numbers.length; i++){
if(numbers[i] < 0 || numbers[i] >= numbers.length){
return -1;
}
while(numbers[i] != numbers[numbers[i]]){
swap(numbers, i, numbers[i]);
}
}
for(int i = 0; i < numbers.length; i++){
if(i != numbers[i]){
return numbers[i];
}
}
return -1;
}
private void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
网友评论