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

For example,
Given nums = [0, 1, 3] return 2.

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

思路:给出一个数组,该数组中的元素各不相同,找出数组中缺少的值并返回。

XOR

XOR在C语言中是位操作,异或。有一个非常神奇的用法:a^b^b=a,a^b^c^b^a=a^a^b^b^c=c可以交换顺序,随意组合,而且一般用于加密算法。也就是说这种方法对顺序没有要求。

int missingNumber(vector<int>& nums) {
int result = ;
for (int i = ; i < nums.size(); i++)
result ^= nums[i]^(i+);
return result;
/*
int result = nums.size();
for(int i = 0; i < nums.size(); i++){
result ^= nums[i] ^ i;
}
return result;
*/
}

SUM

这个方法真的是应该最容易想到的,sum的类型设置应该为long,不然会溢出。

int missingNumber(vector<int >nums) { //sum
int len = nums.size();
int sum = (+len)*(len+)/;
for(int i=; i<len; i++)
sum-=nums[i];
return sum;
}

Binary Search

这种方法对一个数组进行二分查找,因为排序之后,下标和元素值应该是相互对应的,如果缺少某个值。

int missingNumber(vector<int >nums) {
int left = , right = nums.size(), mid = (left+right)/;
while(left < right){
mid = (left+right)/;
if(num[mid] > mid)
right = mid;
else
left = mid +;
}
return left;
}

[Array]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. LeetCode Array Easy 268. Missing Number

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

  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序列中遗失的数字

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

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

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

  9. 268. Missing Number

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

随机推荐

  1. Win10系统无法安装可选功能提示错误代码0x800F081F的解决方法

    DISM /Online /Cleanup-Image /RestoreHealth /Source:wim:H:\sources\install.wim:1 /limitaccess

  2. (function($){….})(jQuery)与$(function(){})的区别

    function fun($){…};fun(jQuery);这种方法多用于存放开发的插件,执行其中的代码时,Dom对象并不一定加载完毕. $(function(){})等价于$(document). ...

  3. amazeUI tab禁止左右滑动(触控操作)

    参考:http://amazeui.clouddeep.cn/javascript/tabs/ 效果: html: <!DOCTYPE html> <html> <hea ...

  4. iOS逆向系列-Reveal

    概述 Reveal是一款调试iOS程序UI界面的神器. 官网地址:https://revealall.com 下载:https://revealapp.com/download/ 建议下载Reveal ...

  5. MapReduce工作流程

  6. (转)NodeJS - 第一个应用程序Hello World

    安装NodeJs 在创建实际的“Hello,World!”应用之前,我们应该先安装NodeJS,安装NodeJS可以访问NodeJS官网,下载相应系统的NodeJS的安装包,进行安装. 程序组件 关于 ...

  7. 关于Windows10企业版的激活方法

    今天打开Excel在使用的时候,突然弹出弹窗,说我的激活即将过期什么的,让我转到设置进行激活. 第一个想到的办法就是更换产品密钥,在网上找了不少产品密钥,密钥有效,但是需要连接企业激活什么的,因为我是 ...

  8. arp协议简单介绍

    1. 什么是ARP? ARP (Address Resolution Protocol) 是个地址解析协议.最直白的说法是:在IP以太网中,当一个上层协议要发包时,有了该节点的IP地址,ARP就能提供 ...

  9. 【左偏树】[LuoguP1456] Monkey King

    多...多组数据... awsl 死命的MLE,原来是忘记清空数组了.... 左偏树模板? 对于每一个操作,我们把两个节点$x,y$的祖先$fx,fy$找到,然后把他们的左右儿子分别合并 最后把$v[ ...

  10. 洛谷P3239 [HNOI2015]亚瑟王

    题目描述 小 K 不慎被 LL 邪教洗脑了,洗脑程度深到他甚至想要从亚瑟王邪教中脱坑.他决定,在脱坑之前,最后再来打一盘亚瑟王.既然是最后一战,就一定要打得漂亮.众所周知,亚瑟王是一个看脸的游戏,技能 ...