leetcode_268.missing number
给定一个数组nums,其中包含0--n中的n个数,找到数组中没有出现的那个数。
解法一:cyclic swapping algorithm
class Solution
{
public:
int missingNumber(vector<int>& nums)
{
nums.push_back(-);
int len=nums.size();
for(int i=;i<len;i++)
{
while(nums[i]>= && nums[i]!=i)
swap(nums[i], nums[nums[i]]);
}
for(int i=;i<=len;i++)
if(nums[i]==-)
return i;
return -;
}
};
解法二:用(1+n)*n/2减掉数组中所有数,就是没有出现的那个数。
class Solution
{
public:
int missingNumber(vector<int>& nums)
{
int n=nums.size(), sum = (+n)*n/;
for(int i:nums)
sum -= i;
return sum;
}
};
解法三:使用异或运算符,a^b^b=a。
class Solution
{
public:
int missingNumber(vector<int>& nums)
{
int result = nums.size();
for(int i=;i<nums.size();i++)
result = result^i^nums[i];
return result;
}
};
leetcode_268.missing number的更多相关文章
- Leetcode-268 Missing Number
#268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...
- Missing number
Missing number 题目: Description There is a permutation without two numbers in it, and now you know wh ...
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- hdu 5166 Missing number
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5166 Missing number Description There is a permutatio ...
- Missing Number, First Missing Positive
268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find th ...
- HDU 5166 Missing number 简单数论
Missing number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) [ ...
- Missing number in array
Given an array of size n-1 and given that there are numbers from 1 to n with one missing, the missin ...
- PAT 1144 The Missing Number
1144 The Missing Number (20 分) Given N integers, you are supposed to find the smallest positive in ...
- [LintCode] Find the Missing Number 寻找丢失的数字
Given an array contains N numbers of 0 .. N, find which number doesn't exist in the array. Example G ...
随机推荐
- 「网络流24题」「LuoguP4015」 运输问题
Description W 公司有 m 个仓库和 n 个零售商店.第 i 个仓库有 ai 个单位的货物:第 j 个零售商店需要 bj 个单位的货物. 货物供需平衡,即 ∑ai=∑bj . 从第 i ...
- 【Codeforces 776B】Sherlock and his girlfriend
[题目链接] 点击打开链接 [算法] 将所有质数染成1,合数染成2即可 [代码] #include<bits/stdc++.h> using namespace std; #define ...
- Watir: Watir-WebDriver对富文本编辑器的定位于Watir是不一致的。
Watir对富文本编辑,一般可以采用b.frame().document.body.innerText = "Value you want to insert"但是Watir-We ...
- C - Soldier and Cards
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Two bo ...
- bzoj 2959: 长跑【LCT+并查集】
如果没有环的话直接LCT 考虑有环怎么办,如果是静态的话就tarjan了,但是这里要动态的缩环 具体是link操作的时候看一下是否成环(两点已联通),成环的话就用并查集把这条链缩到一个点,把权值加给祖 ...
- Codeforces732D Exams
显然要二分答案,然后对于一个天数,我们来判断是否可以通过所有考试,这里就贪心来安排就好了. 首先我们希望每门课的考试时间越晚越好,然后就是先复习最早开始的考试. #include <bits/s ...
- SequoiaDB、SequoiaSQL、Cloudera Manager4.8.0、Cloudera CDH4.5 详细安装教程
1安装SequoaiDB集群 1.1配置信任关系 以root用户执行下面的操作 1 执行命令 ssh-keygen 然后一直回车确定即可 2 每台机器都打开id_rsa.pub文件 vi ~/.ssh ...
- GoAhead4.1.0 开发总结三(GoAction+Ajax实现局部数据交互)
环境 官方文档:https://www.embedthis.com/goahead/doc/ 源码下载: goahead-4.1.0-src.tgz 系统平台:Ubuntu 12.04.4 gcc v ...
- 二分搜索 2015百度之星初赛1 HDOJ 5248 序列变换
题目传送门 /* 二分搜索:在0-1e6的范围找到最小的max (ai - bi),也就是使得p + 1 <= a[i] + c or a[i] - c 比赛时以为是贪心,榨干智商也想不出来:( ...
- 480 Sliding Window Median 滑动窗口中位数
详见:https://leetcode.com/problems/sliding-window-median/description/ C++: class Solution { public: ve ...