lintcode-medium-Find the Missing Number

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;

    }
}
时间: 2024-10-13 11:31:31

lintcode-medium-Find the Missing Number的相关文章

[LintCode] Find the Missing Number 寻找丢失的数字

Given an array contains N numbers of 0 .. N, find which number doesn't exist in the array. Example Given N = 3 and the array [0, 1, 3], return 2. Challenge Do it in-place with O(1) extra memory and O(n) time. 这道题是LeetCode上的原题,请参见我之前的博客Missing Number

[LintCode] Find the Missing Number II

Giving a string with number from 1 to n in random order, but miss 1 number.Find that number. You can assume n <= 30 Example Given n = 20, str = 19201234567891011121314151618 return 17 Solution 1. DFS Algorithm. 1. set a 1D boolean array: exist[0.....

LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. (Easy) 分析:求n的阶乘中末位0的个数,也就是求n!中因数5的个数(2比5多),简单思路是遍历一遍,对于每个数,以此除以5求其因数5的个数,但会超时. 考虑到一个数n比他小

&lt;LeetCode OJ&gt;Missing Number【268】

268. Missing Number My Submissions Question Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium 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 num

&amp;lt;LeetCode OJ&amp;gt; 268. Missing Number

268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium 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.

leetcode Missing Number

题目连接 https://leetcode.com/problems/missing-number/ Missing Number Description 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$. Not

hdu 5166 Missing number

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5166 Missing number Description There is a permutation without two numbers in it, and now you know what numbers the permutation has. Please find the two numbers it lose. Input There is a number T shows t

【LeetCode】268. Missing Number

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 i

HDU - 5166 - Missing number &amp;&amp; 5167 - Fibonacci

Missing number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 430    Accepted Submission(s): 233 Problem Description There is a permutation without two numbers in it, and now you know what num

LeetCode:Missing Number - 缺失的数字

1.题目名称 Missing Number (缺失的数字) 2.题目地址 https://leetcode.com/problems/missing-number 3.题目内容 英文:Given an array containing n distinct numbers taken from 0, 1, 2, ..., n  find the one that is missing from the array. 中文:给出一个包含了n个不同数字的数组,从0开始一直到n,找出缺失的数字.如果数