[Array]268. Missing Number
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的更多相关文章
- <LeetCode OJ> 268. Missing Number
268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- 268. Missing Number@python
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- LeetCode Array Easy 268. Missing Number
Description Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one th ...
- Java [Leetcode 268]Missing Number
题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
- [LeetCode] 268. Missing Number ☆(丢失的数字)
转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...
- 268. Missing Number序列中遗失的数字
[抄题]: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
- 268. Missing Number -- 找出0-n中缺失的一个数
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
随机推荐
- 解决jquery ajax在跨域访问post请求的时候,ie9以下无效(包括ie9)的问题
最近在做项目的时候遇到一个问题,就是跨域请求ajax的时候ie9以下的浏览器不可以访问,直接执行error里面的代码,但是也不报错,就上网查了查,发现了一个很好用的方法,在这里记录一下,也希望可以帮到 ...
- Error:(27, 13) Failed to resolve: com.android.support.constraint:constraint-layout:1.0.2约束布局constraint-layout导入失败的解决方案
运行demo提示错误: Error:(27, 13) Failed to resolve: com.android.support.constraint:constraint-layout:1.0.2 ...
- Ubuntu 18.04 Linux上安装Etherpad,基于Web的实时协作编辑器
介绍 Etherpad是一个开源的,基于Web的实时协作编辑器(http://www.0834nanke.com) 它允许多个人使用他们的Web浏览器同时编辑文档. 它还提供了一些很酷的功能,如富文本 ...
- Linux中要重启apache服务与在windows是有很大的区别,下面我们来介绍一下
在Linux中要重启apache服务与在windows是有很大的区别,下面我们来介绍一下常用的命令,需要的朋友参考下吧(http://www.hnkjlb.com) linux系统为Ubuntu 一. ...
- springmvc jar包下载
(1) springmvc jar包下载 提供地址 - qq_31307253的博客 - CSDN博客https://blog.csdn.net/qq_31307253/article/details ...
- php判断一个值是否在一个数组中,区分大小写-也可以判断是否在键中
function in_array_case($value,$array){ return in_array(strtolower($value),array_map('strtolower',$ar ...
- System.Web.Mvc.ViewResultBase.cs
ylbtech-System.Web.Mvc.ViewResultBase.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, Pub ...
- 左神算法书籍《程序员代码面试指南》——1_01设计一个有getMin功能的栈
[题目] 实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作. [要求] 1.pop.push.getMin操作的时间复杂度都是O(1).2.设计的栈类型可以使用现成的栈结构. ...
- Java之io nio aio 的区别
这个问题最近面试总是遇到,作为一个只会写流水代码的程序员,一脸懵逼.看了网上的解释,看的还是很模糊,说下我对这个的理解. 先引出一个话题,两个大水缸,一个空一个满,让你把一个缸里面的水弄到另一个里面. ...
- linux 详解
一.日常使用命令/常用快捷键命令开关机命令 1.shutdown –h now:立刻进行关机 2.shutdown –r now:现在重新启动计算机 3.reboot:现在重新启动计算机 ...