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. 简单的UDP服务端和客户端示例

    UDP的理论不再多说,我这里直接给出一个关于UDP的HelloWorld程序,代码明了,希望对刚入门的学生有所帮助! 当然,实际上,在这块我也刚入门! 首先写服务端代码,服务端邦定本地的IP和端口来监 ...

  2. Java开发系列-文件上传

    概述 Java开发中文件上传的方式有很多,常见的有servlet3.0.common-fileUpload.框架.不管哪种方式,对于文件上传的本质是不变的. 文件上传的准备 文件上传需要客户端跟服务都 ...

  3. Leetcode93. Restore IP Addresses复原IP地址

    给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", ...

  4. html-from提交表单

    使用form创建的仅仅是一个空白的表单, 我们还需要向form中添加不同的表单项 <!DOCTYPE html> <html> <head> <meta ch ...

  5. HTML - 文本标签相关

    <html> <head></head> <body> <!-- 标题标签 : h1到h6, 文字大小依次变小, 加粗显示, 自带换行 标签中的部 ...

  6. 84 落单的数 III

    原题网址:http://www.lintcode.com/zh-cn/problem/single-number-iii/# 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到 ...

  7. PAT甲级——A1097 Deduplication on a Linked List

    Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...

  8. HDU--2602(0-1背包)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2602  分析:一个0-1背包问题.记得<背包九讲>的方法. dp[j]=max{dp[j],d ...

  9. css 渐变背景

    background: linear-gradient(left,#fa7f6d, #fc5e7f); left: 从左边开始

  10. filters和scope在ElasticSearch Faceting模块的应用

    filters和scope在ElasticSearch Faceting模块的应用 使用ElasticSearch的Facet功能时,有一些关键点需要记住.首先,faceting的结果只会基于查询结果 ...