题目链接:https://leetcode.com/problems/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
.
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++) { //检查数组是否连续 if((startData+1)==nums[i]) { startData=nums[i]; } else { return startData+1; } } /** * 假设数组是连续的 * 起始值不是0。则返回0,否则返回数组末尾数的下一个自然数 */ if(startData==nums[nums.length-1]) { if(nums[0]>0) return 0; else return nums[nums.length-1]+1; } return 0; } }