码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode: MissingNumber

时间:2015-08-25 21:51:39      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

题目链接: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;
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode: MissingNumber

标签:

原文地址:http://blog.csdn.net/yangyao_iphone/article/details/47981919

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!