Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

Example 1:

Input: [3,0,1]
Output: 2

Example 2:

Input: [9,6,4,2,3,5,7,0,1]
Output: 8

Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

解法考虑两种:数学解法和数组解法,数学解法利用连续n个数的序列特点,求和后求差计算,O(n)时间复杂度

class Solution {
public int missingNumber(int[] nums) {
int sum = (0 + nums.length) * (nums.length + 1) / 2;
for (int i = 0; i < nums.length; i++){
sum = sum - nums[i];
}
return sum;
} // public int missingNumber(int[] nums) {
// boolean[] bit = new boolean[nums.length+1];
// for (int i = 0; i < nums.length; i++) {
// bit[nums[i]] = true;
// }
// int i = 0;
// while(bit[i] == true) {
// i++;
// }
// return i;
// }
}

  

[leetcode268]Missing Number的更多相关文章

  1. Leetcode-268 Missing Number

    #268.  Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...

  2. 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!. N ...

  3. Missing number

    Missing number 题目: Description There is a permutation without two numbers in it, and now you know wh ...

  4. 【LeetCode】268. Missing Number

    Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...

  5. hdu 5166 Missing number

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5166 Missing number Description There is a permutatio ...

  6. Missing Number, First Missing Positive

    268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find th ...

  7. HDU 5166 Missing number 简单数论

    Missing number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) [ ...

  8. Missing number in array

    Given an array of size n-1 and given that there are numbers from 1 to n with one missing, the missin ...

  9. PAT 1144 The Missing Number

    1144 The Missing Number (20 分)   Given N integers, you are supposed to find the smallest positive in ...

随机推荐

  1. Linux中Nginx安装教程

    Nginx 是一个很强大的高性能Web和反向代理服务器,它具有很多非常优越的特性: 在连接高并发的情况下,Nginx是Apache服务器不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的 ...

  2. C语言之标准头文件模板

    /*======================================================================================* * 版权 : xxx ...

  3. Python之Unittest和Requests库详解

    1.按类来执行 import unittest class f1(unittest.TestCase): def setUp(self): pass def tearDown(self): pass ...

  4. Android横竖屏切换生命周期变化

    1. AndroidMenifest没有设置configChanged属性. 竖屏启动(横屏启动相同): onCreate -->onStart-->onResume 切换横屏: onPa ...

  5. npm与cnpm的install无反应

    问题描述 1.npm -v检查版本正常,npm install安装依赖提示超时 2.cnpm -v检查版本正常,cnpm install安装依赖无反应(输入命令后没有任何提示,一直卡在那) 解决(参考 ...

  6. CentOS7.2 1511部署RabbitMQ

    一.安装RabbitMQ依赖的的Erlang最小支持包(当然也可以安装完整的Erlang) 1.下载RabbitMQ的Erlang最小支持包源文件 git clone https://github.c ...

  7. Disk

    一.简介 二.其他 1)Disk I/O

  8. checkinstall

    一.简介 Linux 的源码安装工具 CheckInstall

  9. 多个表左联,要返回全部的结果,解决不能用where的问题

    qb.leftJoin('info_student', 'grouping_class_student.studentId', 'info_student.id'); qb.leftJoin('gro ...

  10. Handler processing failed; nested exception is java.lang.NoSuchMethodError: org.apache.commons.codec.digest.DigestUtils.sha1Hex(Ljava/lang/String;)Ljava/lang/String;

    异常:Handler processing failed; nested exception is java.lang.NoSuchMethodError: org.apache.commons.co ...