标签:
题目链接: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个数的数组中找出丢失的那个
分析:很简单,排下序,遍历一下即可
代码:
public class Solution {
public int missingNumber(int[] nums) {
int k = 0;
int len = nums.length;
Arrays.sort(nums);
for(int i=0; i<nums.length; i++) {
if(nums[i] != k)
return k;
k++;
}
if(k == len) {
return k;
}
return 0;
}
}版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/yangyao_iphone/article/details/47981919