<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 ...
随机推荐
- CSS-类和ID选择器的区别
学习了类选择器和ID选择器,我们会发现他们之间有很多的相似处,是不是两者可以通用呢?我们不要着急先来总结一下他们的相同点和不同点: 相同点:可以应用于任何元素不同点: 1.ID选择器只能在文档中使用一 ...
- C#语言最基础的认识变量
变量是指一块存储数据的内存空间,并且该内存区域的数据内容可以发生变化. 变量是必须先声明后赋值. 基本的语法:数据类型+变量名: 变量名=数据: 简单的使用技巧:声明赋值简写 Static void ...
- js基础---数字日期及运算
显示年月日 var a=new Date; console.log(a); var year=a.getFullYear(); var month=a.getMonth()+1; var day=a. ...
- phpstorm如何在同一个文件夹打开多个目录
phpstorm默认一个窗口只显示一个项目,如果新建一个项目,他会给你个选项卡,问你是在新窗口打开新项目还是在本窗口打开. 能不能在一个窗口打开多个项目呢?就像sublime text那样,其实是可以 ...
- docloud后台管理项目(前端篇)
以下内容与主题无关,如果不想看可以直接忽视 !--忽视开始--! 给大家推荐一款强大的编辑器,那就是集响应快.体验好.逼格高.功能丰富为一体的sublime text 3.它除了以上特点,还有一个最重 ...
- 浅谈jQuery宽高及其应用
[前言] 今天讲了讲jQuery各种元素宽高的获取和设置,下面简单总结下,希望对各位小伙伴有所帮助 [主体] 补充知识点: (1)width()返回结果无单位,css("width" ...
- Hibernate连接池断开自动重连
异常: javax.servlet.ServletException: org.springframework.transaction.CannotCreateTransactionException ...
- Win32 线程同步
Win32 线程同步 ## Win32线程同步 ### 1. 原子锁 ### 2. 临界区 {全局变量} CRITICAL_SECTION CS = {0}; // 定义并初始化临界区结构体变量 {线 ...
- 洛谷P1598 垂直柱状图
模拟题...我自己一直被光标下去上不去怎么模拟困扰,实际上可以直接从高到低,从左到右模拟 我的代码(算法借鉴题解) #include <bits/stdc++.h> using names ...
- windows桌面远程工具连接Ubuntu
1.Ubuntu安装:sudo apt-get install xrdp sudo apt-get install vnc4server sudo apt-get install xubuntu ...