Given an array containing n distinct numbers taken from 0, 1, 2, ..., n
,
find the one that is missing from the array.
For example,
Given nums = [0, 1, 3]
return 2
.
二分查找
public class Solution {
public int missingNumber(int[] nums) {
Arrays.sort(nums);
int result=0;
int start=0;
int end=nums.length-1;
while (start < end) {
int mid = start + (end-start)/2;
if (nums[mid] == mid) {
start = mid+1;
}else{
end = mid-1;
}
}
return nums[start] == start ? start+1 : start;
}
}