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?
思路:蛮水的这题。。。但是还是随便写写。需要注意的是给你的数组并非按顺序排的。所以一开始先排序下。。
1 class Solution { 2 public int missingNumber(int[] nums) { 3 Arrays.sort(nums); 4 int i; 5 for (i=0;i<nums.length;i++){ 6 if (i!=nums[i]) 7 break; 8 } 9 return i; 10 } 11 }