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?

Solution 1:

class Solution {
public int missingNumber(int[] nums) {
int sum = 0;
for (int i = 0; i <= nums.length; i++) {
sum += i;
}
for (int num : nums) {
sum -= num;
}
return sum;
}
}

Solution 2:

class Solution {
public int missingNumber(int[] nums) {
int res = nums.length;
for (int i = 0; i< nums.length; i++) {
res ^= i ^ nums[i];
}
return res;
}
}

[LC] 268. Missing Number的更多相关文章

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

    268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...

  2. 【LeetCode】268. Missing Number

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

  3. 268. Missing Number@python

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

  4. 268. Missing Number序列中遗失的数字

    [抄题]: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...

  5. Java [Leetcode 268]Missing Number

    题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...

  6. [LeetCode] 268. Missing Number ☆(丢失的数字)

    转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...

  7. 268. Missing Number -- 找出0-n中缺失的一个数

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

  8. 268. Missing Number

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

  9. 33. leetcode 268. Missing Number

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

随机推荐

  1. keras_yolo3阅读

    源码地址 https://github.com/qqwweee/keras-yolo3 春节期间仔细看了看yolov3的kears源码,这个源码毕竟不是作者写的,有点寒酸,可能大道至简也是这么个理.我 ...

  2. 洛谷 P1341 无序字母对(欧拉回路)

    题目传送门 解题思路: 一道欧拉回路的模板题,详细定理见大佬博客,任意门 AC代码: #include<cstdio> #include<iostream> using nam ...

  3. 《Docekr入门学习篇》——Docker简介

    Docker简介 什么是docker Docker是Docker.inc公司开源的一个基于LXC技术之上构建的Container容器引擎,源代码托管在GitHub上,基于Go语言并遵从Apache2. ...

  4. cisco路由器license的相关命令简单梳理(转)

    转自https://blog.51cto.com/legendland/1900185作者:legendlandlicense:对于IP Base基本的IOS功能外,另外三个技术包(1 数据Data: ...

  5. 吴裕雄--天生自然 JAVA开发学习:String 类

    public class StringDemo{ public static void main(String args[]){ char[] helloArray = { 'r', 'u', 'n' ...

  6. spring boot集成MyBatis 通用Mapper 使用总结

    spring boot集成MyBatis 通用Mapper 使用总结 2019年 参考资料: Spring boot集成 MyBatis 通用Mapper SpringBoot框架之通用mapper插 ...

  7. Jupyer Notebook, Jupyter Lab 虚拟环境配置

    虚拟环境 conda create -n python36 python=3.6 使用以下命令激活: activate python36 Notebook 安装插件 conda install nb_ ...

  8. c#学习笔记03——委托和事件

    委托:一种引用类型,这种类型可以用来定义方法签名,从而使用委托实现将方法作为参数传递给其他方法.类似于C++中的函数之争,使用委托使程序员可以将方法引用封装在委托对象内. 定义和声明委托: deleg ...

  9. session和token区别

    分布式系统认证/授权目前分布式系统存在两种常用的认证授权方式:分布式session和token 1.session的概念 session中存放登录用户的个人信息,创建session时,随机生成一个se ...

  10. MySQL——事务(transaction)简单总结

    简介: MySQL事务操作主要用于处理操作量大,复杂度高的数据,比如说,在人员管理系统中要删除一个人员,你既要删除他的基本资料,也要删除该人员的相关信息,如文章.信箱等.这些数据库操作语句就构成了一个 ...