題目:Given an array containingndistinct numbers taken from0, 1, 2, ..., n
, find the one that is missing from the array.
For example,
Givennums=[0, 1, 3]
return2
.
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
解題思路:題意為給定一個包含n個不重復的數的數組,從0,1,2...n,找出數組中遺漏的那個數。示例代碼如下:
public class Solution { public int missingNumber(int[] nums) { //首先對數組進行排序 Arrays.sort(nums); int startData=nums[0]; for(int i=1;i<nums.length;i++) startdata="=nums[nums.length-1])" else="" return="">0) return 0; else return nums[nums.length-1]+1; } return 0; } }</nums.length;i++)>