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.
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
找到順序數(shù)列0,1,2,...n 中缺失的一位筝尾。關(guān)鍵在于如何不使用額外空間噪沙。
排好序遍歷即可。
class Solution {
public int missingNumber(int[] nums) {
if(nums == null) return 0;
Arrays.sort(nums);
for(int i = 0 ; i< nums.length ; i++){
if( nums[i] != i){
return i;
}
}
return nums.length;
}
}