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 ...
随机推荐
- [Selenium] 使用Firefox Driver 示例
//导入Selenium 库和FirefoxDriver 库 package com.learningselenium.simplewebdriver; import org.openqa.selen ...
- 「SHOI2007」「Codevs2341」 善意的投票(最小割
2341 善意的投票 2007年省队选拔赛上海市队选拔赛 时间限制: 5 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 幼儿园里有n个小朋 ...
- linux--memcache的安装和使用(转)
memcache是高性能,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度.据说官方所说,其用户包括twitter.digg.flickr等,都是些互联网大腕呀.目前用memca ...
- B - Mike and Fun
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Mike a ...
- Table View Programming Guide for iOS---(六)---A Closer Look at Table View Cells
A Closer Look at Table View Cells A table view uses cell objects to draw its visible rows and then c ...
- centos7无故重启-内核升级
最近有一台物理服务器,centos7操作系统,无故重启,每天都会发生这种情况: 解决: 升级内核 CentOS 允许使用 ELRepo,这是一个第三方仓库,可以将内核升级到最新版本,使用ELRepo升 ...
- POJ3468【线段树lazy操作】
上午理论AC,打到现在快吐了... 一个那么**Lazy操作打成这样,query操作和update操作都有问题,妈蛋,发现是mid<=s+1-真是蠢到家,明明是mid+1<=s卧槽连左和右 ...
- PTA 水...
习题4-2 求幂级数展开的部分和 (20分) 已知函数e^x可以展开为幂级数1+x+x2/2!+x3/3!+⋯+xk/k!+⋯1+x+x^2 /2! + x^3 /3! + \cdots + x^k ...
- SpringBoot整合Spring Data Solr
此文不讲solr相关,只讲整合,内容清单如下 1. maven依赖坐标 2. application.properties配置 3. Java Config配置 1. maven坐标 <depe ...
- A.华华听月月唱歌
链接:https://ac.nowcoder.com/acm/contest/392/A 题意: 月月唱歌超级好听的说!华华听说月月在某个网站发布了自己唱的歌曲,于是把完整的歌曲下载到了U盘里.然而华 ...