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. @JsonIgnoreProperties 不生效

    定义类如下, import org.codehaus.jackson.annotate.JsonIgnoreProperties; import org.codehaus.jackson.annota ...

  2. share团队冲刺6

    团队冲刺第六天 昨天:进行各种原件的自定义样式,进行界面布局 登陆界面: 今天:进行后台的代码编写,实现各种按钮的功能 问题:在不同的型号手机上,界面会发生不兼容的问题.

  3. PHP时间戳常用转换

    //设置中国时区 date_default_timezone_set('PRC'); //今天的时间搓 $today_start = strtotime(date('Y-m-d',time()).' ...

  4. dfs--八皇后问题

    在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对于给定的N,求出有多少种合法的放置方法. 因为我们 ...

  5. jquery判断字符串中是否包含特定字符的方法总结

    方法一:使用indexOf() 和lastIndexOf()方法 案例: var Cts = "bblText"; if(Cts.indexOf("Text") ...

  6. KVM---利用 libvirt+qemu-kvm 创建虚拟机

    KVM 虚拟化已经是一个工业级的虚拟化解决方案了,以前都是直接下载 VMware,然后安装其他操作系统的,今天我们来体验一下自己动手创建一台虚拟机,这样你就会知道在KVM下创建一台虚拟机,是多么简单的 ...

  7. SpringBoot2.0整合Quartz实现动态设置定时任务时间

    一.    引入依赖 <!-- 引入quartz依赖 --> <dependency> <groupId>org.springframework.boot</ ...

  8. 用IDLE调试python程序

    1. 设置断点 先放例子: import pdb a=1 b=10 pdb.set_trace()#这里是断点 c=a+b print(c) import pdb 后,用pdb.set_trace() ...

  9. Python—快速排序算法

    # _*_ coding=utf-8 _*_ """ 快速排序: 通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比 另外一部分的所有数据都要小,然后 ...

  10. leetcode腾讯精选练习之最长公共前缀(九)

    最长公共前缀 题目 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower"," ...