[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 ...
随机推荐
- scrapy运行的整个流程
Spiders: 负责处理所有的response,从这里面分析提取数据,获取Item字段所需要的数据,并将需要跟进的URL提交给引擎,再次进入到Scheduler调度器中 Engine: 框架的核心, ...
- Foundation框架系列-NSDictionary
排序 对字典中的key按照字母升序排序 // NOTE: 排序,得出最终请求字串 NSArray* sortedKeyArray = [[tmpDict allKeys] sortedArrayUsi ...
- MySQL其他和备份
目录 事务 存储引擎 InnoDB存储引擎 数据存储形式 锁的粒度 事务 数据的存储特点 MyISAM存储引擎 数据存储形式 锁的粒度 事务 数据的存储特点 其他 对比与选择 视图 触发器 存储过程 ...
- Django + Uwsgi + Nginx 的生产环境部署实战
目录 Django + Uwsgi + Nginx 的生产环境部署实战 安装Uwsgi 一.使用命令来启动django项目 二.使用配置文件来启动我们的Django项目 安装Nginx 配置Nginx ...
- IDEA Error:java: Compilation failed: internal java compiler error 解决方案
这是由于版本不一致导致的 file => settings => 搜索找到Java Compiler 把相应jdk版本改成1.8 ctrl+alt+s
- 安装Ubuntu16.04卡在logo界面
问题背景 笔者在使用U盘UEFI模式安装Ubuntu16.04时,遇到一个问题,即在BIOS里的boot设置U盘为第一启动项之后,启动,并没有顺利进入系统,而是卡在了logo界面.(PS:其实我等了它 ...
- Installer - 使用Maven自动布署至外部Tomcat
一.配置相关文件 1.配置tomcat的conf/tomcat-users.xml文件 <tomcat-users> <role rolename="manager-scr ...
- Spring MVC(五)--控制器通过注解@RequestParam接受参数
上一篇中提到,当前后端命名规则不一致时,需要通过注解@RequestParam接受参数,这个注解是作用在参数上.下面通过实例说明,场景如下: 在页面输入两个参数,控制器通过注解接受,并将接受到的数据渲 ...
- SpringBoot 01_HelloWorld
本文环境配置: JDK:1.8 开发工具:IDEA 操作系统:Windows10 集成工具:Maven SpringBoot版本:1.5.6.RELEASE 构件方式:Spring Initializ ...
- CentOS 8上安装Docker
前言 这几天,想玩玩docker,方便漏洞复现,我去学docker搭建了,感觉不错,挺方便的 安装步骤: 1.下载docker-ce的repo curl https://download.docker ...