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. java第二章继承

    继承(extends)面向对象三大特征之一 类中有许多相同的属性和方法,代码重复,如果需要修改涉及较多类修改量增多 将子类中共有的属性和方法提取到父类,让子类继承父类,减少代码量,扩展性维护性,子类可 ...

  2. DNS 原理

    一.DNS 是什么? DNS (Domain Name System 的缩写)的作用非常简单,就是根据域名查出IP地址.你可以把它想象成一本巨大的电话本. 举例来说,如果你要访问域名math.stac ...

  3. forEach() 和 map() 遍历

    1.forEach()   没有返回值. arr[].forEach(function(value,index,array){ //do something }) 参数:value数组中的当前项, i ...

  4. C++ map 使用erase在windows下崩溃,在linux下没有问题的原因

    注意:此程序在win环境下会出现上述描述的问题:在mac环境下第一种方式是正常运行的.Map.erase有3个重载函数: void erase(iterator position); size_typ ...

  5. 贪吃蛇(简易版)Leslie5205912著

    # include <stdio.h># include <string.h># include <windows.h># include <stdlib.h ...

  6. Linux驱动之触摸屏程序编写

    本篇博客分以下几部分讲解 1.介绍电阻式触摸屏的原理 2.介绍触摸屏驱动的框架(输入子系统) 3.介绍程序用到的结构体 4.介绍程序用到的函数 5.编写程序 6.测试程序 1.介绍电阻式触摸屏的原理 ...

  7. zabbix的api接口

    zabbix官方文档解释,api是开发者能获得修改zabbix配置,获取历史数据.主要用于: 1.创建新应用 2.集成zabbix与第三方软件 3.自动运行任务 运用JSON-RPC2.0协议,因此接 ...

  8. EasyPR源码剖析(1):概述

    EasyPR(Easy to do Plate Recognition)是本人在opencv学习过程中接触的一个开源的中文车牌识别系统,项目Git地址为https://github.com/liuru ...

  9. jmeter安装与使用

    1.下载安装Jmeter.JDK Jmeter官网下载地址: http://jmeter.apache.org/download_jmeter.cgi JDK官网下载地址: http://www.or ...

  10. Android自动化之Monkey测试(二)

    本文主要从以下方面进行分享. 一.查看应用包名二.Monkey启动三.Monkey停止四.Monkey命令五.日志分析 一.查看应用包名 大多数时候,我们都是对特定的应用进行monkey测试,因此需要 ...