<LeetCode OJ> 268. Missing Number
268. Missing Number
Submissions: 83547 Difficulty: Medium
Given an array containing n distinct numbers taken from 0,, find the one that is missing from the array.
1, 2, ..., n
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?
分析:DONE
我真蛋疼。题意理解错了,我还以为是干嘛呢!
后来才明确原来是随机从0到size()选取了n个数,当中仅仅有一个丢失了(显然的)。
别人的算法:数学推出,0到size()的总和减去当前数组和sum
class Solution {
public:
int missingNumber(vector<int>& nums) {
int sum = 0;
for(int num: nums)
sum += num;
int n = nums.size();
return (n * (n + 1))/ 2 - sum;
}
};
这道问题被标注为位运算问题:參考讨论区的位运算解法:
这个异或运算曾经用到过,到这道题还是想不起这种方法,我真是日了狗了!
异或运算xor。
0 ^ a = a ^ 0 =a
a ^ b = b ^ a
a ^ a = 0
0到size()间的全部数一起与数组中的数进行异或运算,
由于同则0,0异或某个未出现的数将存活下来
class Solution {
public:
int missingNumber(vector<int>& nums) {
int res = 0;
for (int i = 1; i <= nums.size(); i++)
res =res ^ i ^ nums[i-1];
return res;
}
};
注:本博文为EbowTang原创,兴许可能继续更新本文。
假设转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50457902
原作者博客:http://blog.csdn.net/ebowtang
<LeetCode OJ> 268. Missing Number的更多相关文章
- [LeetCode&Python] Problem 268. Missing Number
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 ...
- 【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 ...
- 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 (缺失的数字)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] 268. Missing Number ☆(丢失的数字)
转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...
- [LeetCode] 268. Missing Number 缺失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 【LeetCode】268. Missing Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 异或 日期 题目地址:https://leet ...
随机推荐
- JavaScript--关闭窗口(window.close)
close()关闭窗口 用法: window.close(); //关闭本窗口 或 <窗口对象>.close(); //关闭指定的窗口 例如:关闭新建的窗口. <script typ ...
- jsp文件就是Servlet,可以在tomcat里进行查看
D:\apache-tomcat-8.0.21\work\Catalina\localhost\springmvc2\org\apache\jsp\index_jsp.class
- 在vSphere Client上安装虚拟机工具VMware Tools
一.什么是虚拟机工具 VMware Tools是一套安装在虚拟机操作系统中的实用程序.VMware Tools可提高虚拟机的性能,并在 VMware产品中实现多个易于使用的功能. 尽管客户机操作系统在 ...
- [转]linux之ps命令
转自:http://www.cnblogs.com/peida/archive/2012/12/19/2824418.html Linux中的ps命令是Process Status的缩写.ps命令用来 ...
- 如何利用Flashback Query 恢复误删除的数据
网上有很多关于数据回复的文章,这里整理一篇供大家参考,希望能帮助的大家! 推荐一家即时通讯云服务商:www.yun2win.com,功能包含im即时通讯.实时音视频.电子白板.屏幕共享的多种融合通讯云 ...
- 在CentOS下搭建Android 开发环境
在CentOS下搭建Android 开发环境 目录 1.环境搭建 1.1.JDK安装 1.2.Eclipse安装 1.3.ADT安装 1.4.Android SDK安装 1.5.Android NDK ...
- js 闭包 定时器
; !function (win) { ; //内部私有 , ; //内部私有 //test.prototype.tt1 = 0;//共有变量 var test = function () {}; t ...
- 前端精选文摘:css之GFC 神奇背后的原理(整理)
CSS3 Grid Layout Web页面的布局,我们常见的主要有“浮动布局(float)”.“定位布局(position)”.“行内块布局(inline-block)”.“CSS3的多栏布局(Co ...
- [pytorch学习]1.pytorch ubuntu安装
看完了Deep Learning with Python,尝试了部分Keras的demo代码. 感觉Keras虽然容易上手,能够快速搭建出一个通用的模型,但是缺乏对底层的控制. 同时,在使用了自己编译 ...
- JS练习:显示和隐藏
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...