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 ...
随机推荐
- achartengine 实现平行线 动态数据 x轴动态移动
achartengine做平行线的时候经常会遇到: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 at java.ut ...
- ORACLE OCP认证
基本情况介绍 Oracle产品非常多,这里说的是Oracle数据库认证体系. Oracle数据库认证体系包括3层,分别是OCA(助理),OCP(专家),OCM(大师) 一般情况下,需一级一级认证,也就 ...
- Oracle查询指定某一天数据,日期匹配
在做一个功能的时候,需要在oracle数据库中查询指定某一天的数据. 如果是简单的当前日期前后几天,也好办 AND TO_CHAR(Rct.Creation_Date, 'YYYY-MM-DD')=t ...
- Android布局控件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...
- c# ListView
// Attempt to run the file. System.Diagnostics.Process.Start(filename); //folderCol 可以存放一个路径的 栈(用于返回 ...
- linux crontab设置
cron来源于希腊单词chronos(意为“时间”),是linux系统下一个自动执行指定任务的程序.例如,你想在每晚睡觉期间创建某些文件或文件夹的备份,就可以用cron来自动执行. 服务的启动和停止 ...
- 日志管理-NLog日志框架简写用法
本文转载:http://www.blogjava.net/qiyadeng/archive/2013/02/27/395799.html 在.net中也有非常多的日志工具,今天介绍下NLog.NLog ...
- HttpWebRequest在GetResponse时总是超时
最近在通过RESTFUL接口来发布些数据,总是出现请求超时,好不容易找到个靠谱点的了,记下来,回去试下!! “ 问题就是我第一个HttpWebRequest在GetResponse之后,忘记将取得的W ...
- PHP文件缓存类
<?php /** * @desc 文件缓存 */ class Cache{ const C_FILE = '/Runtime/'; private $dir = ''; const EXT = ...
- Nginx 模块开发(1)—— 一个稍稍能说明问题模块开发 Step By Step 过程
1. Nginx 介绍 Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,它的发音为“engine X”, 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/S ...