Missing Number, First Missing Positive
268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
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?
class Solution {
public:
int missingNumber(vector<int>& nums) {
int res = ;
int numsSize = nums.size();
bool isFind = false;
for(int i=;i<numsSize;i++){
while(nums[i]!=i){
if(nums[i] >= numsSize){
isFind = true;
res = i;
break;
}
swap(nums[i],nums[nums[i]]);
}
}
return isFind ? res:numsSize;
}
};
此外,还有很多好方法,例如,
法1.
先计算sum1=0+1+2+3+...+n,
再计算sum2 = nums[0]+nums[1]+...+nums[n-1];
然后sum1-sum2就是缺失的那个数
法2.排序二分
41. First Missing Positive
Given an unsorted integer array, find the first missing positive integer.
For example,
Given [1,2,0] return 3,
and [3,4,-1,1] return 2.
Your algorithm should run in O(n) time and uses constant space.
如果数组中的数是按照数该在的位置摆放(数i摆放在数组i的位置),那么很容易就能获得第一个缺失的正数。
所以我们先调整数组数的位置,令下标为i的位置存放数i。
再遍历一遍数组,如果nums[i]!=i,说明该位置的数缺失。
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int n = nums.size();
int i = ;
while(i<n){
while( nums[i]!= i+ ){
if(nums[i]<= || nums[i]>n || nums[i]==nums[nums[i]-]){
break;
}
swap(nums[i],nums[nums[i]-]);
}
i++;
}
i = ;
while(i<n && nums[i] == i+){
i++;
}
return i+;
}
};
Missing Number, First Missing Positive的更多相关文章
- PAT 1144 The Missing Number
1144 The Missing Number (20 分) Given N integers, you are supposed to find the smallest positive in ...
- PAT 1144 The Missing Number[简单]
1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...
- [PAT] 1144 The Missing Number(20 分)
1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...
- PAT_A1144#The Missing Number
Source: PAT A1144 The Missing Number (20 分) Description: Given N integers, you are supposed to find ...
- PAT 甲级 1144 The Missing Number (20 分)(简单,最后一个测试点没过由于开的数组没必要大于N)
1144 The Missing Number (20 分) Given N integers, you are supposed to find the smallest positive in ...
- PAT(A) 1144 The Missing Number(C)统计
题目链接:1144 The Missing Number (20 point(s)) Description Given N integers, you are supposed to find th ...
- 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 ...
随机推荐
- DevExpress中SearchLookUpEdit用法总结
在前一个项目中用到了DevExpress,需要搜索某一个字段,来拉取出对应的相关信息,比来比去,发现SearchLookUpEdit的用户体验更好,但自己是个不折不扣的C#和DevExpress的初学 ...
- Sql Server获得每个表的行数
SELECT o.[name], ddps.[row_count] FROM sys.indexes AS i INNER JOIN sys.objects AS o ON i.[object_id] ...
- 深入理解JavaWeb技术内幕(一)
最近在看许令波的<深入理解JavaWeb技术内幕>.整理了一些笔记.想做一个系列,这篇是系列的第一篇,讲Web请求. B/S架构 最常见的架构方式. 优点: 1.客户端使用统一(此处的统一 ...
- JavaWeb限流QPS简易框架
Java Web利用filter实现拦截请求,统计信息.并控制单台机器QPS. /** * 网络流量控制器 */ public class TrafficFilter implements Filte ...
- IE9以下通过css让html页面背景图片铺满整个屏幕
第一种方法不设为背景图片,通过css来控制样式,可兼容到IE6,代码如下: <!DOCTYPE html> <html lang="en"> <hea ...
- Android sample 之模拟重力感应,加速度
class SimulationView extends View implements SensorEventListener { // diameter of the balls in meter ...
- Sublime Text 3 个人配置文件
{ "dpi_scale": 1.0, "draw_white_space": "selection", "fallback_en ...
- Linux下静态编译Qt程序
一般情况下,我们用Qt编译出来的程序是要依赖于系统Qt库的,也就是这个程序移到别的没有安装Qt库的系统上是不能使用的.会提示缺少……库文件之类的错误.这就是动态编译的结果. 但是如果我们想编译一个程序 ...
- Tar打包、压缩与解压缩到指定目录的方法
tar在linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数: -c :create 建立压缩档案的参数: -x : 解压缩压缩档案的参数: -z : 是 ...
- BMP彩色转成黑色二值图
一天半把彩色bmp转成黑白了. 原理是: 第一步:读出位图数据的偏移位置:即第11个字节,用fseek即可. 然后将偏移位置之前的数据全部写入新的bmp图中. 第二步:用fseek移到位图数据这前,判 ...