你的位置:首页 > Java教程

[Java教程]268. Missing Number


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.

代码如下:

 1 public class Solution { 2   public int missingNumber(int[] nums) { 3     Arrays.sort(nums); 4     for(int i=0;i<nums.length;i++) 5     { 6       if(nums[i]!=i) 7       return i; 8     } 9     return nums.length;10   }11 }