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. java_序列化

    import java.io.*; class People implements Serializable { /* * 序列化和反序列化的时候,会抛出就NotSerializableExcepti ...

  2. C开发系列-数组

    C语言数组 数组:用来存储一组数据. 计算C语言的数组长度 int age1 = 12; int age2 = 15; int age3 = 10; int age4 = 13; int ages[] ...

  3. Func-Chain.js 另一种思路的javascript异步编程解决方案

    本文转载自:https://www.ctolib.com/panruiplay-func-chain.html Func-Chain.js 另一种思路的javascript异步编程,用于解决老式的回调 ...

  4. 【珍惜时间】vuepro

    老规矩放上大大的github开源地址:https://github.com/goodheart222/vuepro 我们再来看看项目的效果,初步根据效果做到心中有数 看到效果的话,我们会发现,肯定是有 ...

  5. 查看java资源的占用

    1,使用命令top -p <pid> ,显示你的java进程的内存情况,pid是你的java进程号,比如1232,按H,获取每个线程的内存情况3,找到内存和cpu占用最高的线程pid,比如 ...

  6. Codeforces Parking Lot

    http://codeforces.com/problemset/problem/630/I 简单的排列组合,推式子技巧:举一个小样例,看着推,别抽象着推,容易错 #include <iostr ...

  7. differential related impedance and termination

    Impedance (1) Z0 Z0 is the impedance of one T-line while other lines are held at 0. Single end. (2) ...

  8. 流程控制&&函数

    1.if 条件语句 if 判断条件: 执行语句…… elif 判断条件: 执行语句…… else: 执行语句…… 2.for 循环 ''' for 后跟变量名,in 后跟序列,注意加冒号 for 循环 ...

  9. 二分图——poj2446匈牙利算法

    /* 怎么建图: 首先分集合:不能相连的点必然在一个集合里,即对角点 再确定怎么连边: 一个点可以向上下左右连边,如果遇到了洞则不行 dfs(i),让匹配到的点接受i作为match结果 寻找增广路时, ...

  10. light oj 1098 数学规律

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