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

lintcode-medium-Find the Missing Number

时间:2016-03-21 08:11:01      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

Given an array contains N numbers of 0 .. N, find which number doesn‘t exist in the array.

 

Given N = 3 and the array [0, 1, 3], return 2

 

 

public class Solution {
    /**    
     * @param nums: an array of integers
     * @return: an integer
     */
    public int findMissing(int[] nums) {
        // write your code here
        
        if(nums == null || nums.length == 0)    
            return 0;
        
        int sum = 0;
        
        for(int i = 0; i < nums.length; i++){
            sum += nums[i];
        }
        
        int N = nums.length;
        
        return N * (N + 1) / 2 - sum;
        
    }
}

 

lintcode-medium-Find the Missing Number

标签:

原文地址:http://www.cnblogs.com/goblinengineer/p/5300477.html

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