<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 ...
随机推荐
- ACM_Repeating Characters
Repeating Characters Time Limit: 2000/1000ms (Java/Others) Problem Description: For this problem, yo ...
- Python中的Map/Reduce
MapReduce是一种函数式编程模型,用于大规模数据集(大于1TB)的并行运算.概念"Map(映射)"和"Reduce(归约)",是它们的主要思想,都是从函数 ...
- Elasticsearch之CURL命令的version控制
普通关系型数据库使用的是(悲观并发控制(PCC)) 当我们在修改一个数据前先锁定这一行,然后确保只有读取到数据的这个线程可以修改这一行数据 ES使用的是(乐观并发控制(OCC)) ES不会阻止某一数据 ...
- node 第三方包学习
时间格式化 moment var moment = require('moment'); moment().format();
- C#——数据库的访问
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 集合Set、List、Map的遍历方法
package com.shellway.javase; import java.util.ArrayList; import java.util.Collection; import java.ut ...
- 14、Scala类型参数
1.泛型类 2.泛型函数 3.上边界Bounds 4.下边界Bounds 5.View Bounds 6.Context Bounds 7.Manifest Context Bounds 8.协变和逆 ...
- SpringMVC(四)@RequestParam
使用@RequestParam可以将URL中的请求参数,绑定到方法的入参上,并通过@RequestParam的3个参数进行配置 Modifier and Type Optional Element D ...
- C# Request
string type = Request["type"]; //值为null; //?type= 值为""; //?type=12 值为"12&qu ...
- HDU 1907 John(博弈)
题目 参考了博客:http://blog.csdn.net/akof1314/article/details/4447709 //0 1 -2 //1 1 -1 //0 2 -1 //1 2 -1 / ...