206. 缺失数字

作者: 拜仁的月饼 | 来源:发表于2019-11-30 15:05 被阅读0次

这个题只需要用到数学思想即可解决。上代码:

class Solution {
    public int missingNumber(int[] nums) {
        int L = nums.length;
        int sumOfAll = 0, sumOfNums = 0;
        for(int i = 1; i <= L; ++i){
            sumOfAll += i;
        }
        for(int elem : nums){
            sumOfNums += elem;
        }

        return sumOfAll - sumOfNums;
    }
}

相关文章

  • 206. 缺失数字

    这个题只需要用到数学思想即可解决。上代码:

  • 缺失数字

    给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。...

  • 缺失数字

    第一种方法: 这个方法是看的别人的方法,数学真的是一门好学科 还有一种方法是使用枚举,这种方法比较新颖。

  • 缺失数字

    题目:给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那...

  • 位运算

    缺失 268. 缺失数字[https://leetcode-cn.com/problems/missing-num...

  • 查找整数数组中缺失的数字

    数组arr大小为n,取值范围0~n-1,如果数组有重复数字,则某些数字就会缺失,试着找出缺失数字。 思路:如果数组...

  • Leetcode PHP题解--D78 206. Reverse

    D78 206. Reverse Linked List 题目链接 206. Reverse Linked Lis...

  • 查找缺失数字

    给定一个包含 0, 1, 2, ..., n 中 n个数的序列,找出 0 .. n 中没有出现在序列中的那个数。(...

  • [数组]缺失数字

    268. 缺失数字 题目描述 给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n...

  • 【LeetCode】缺失数字

    题目描述: 给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列...

网友评论

    本文标题:206. 缺失数字

    本文链接:https://www.haomeiwen.com/subject/heuowctx.html