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的更多相关文章

  1. PAT 1144 The Missing Number

    1144 The Missing Number (20 分)   Given N integers, you are supposed to find the smallest positive in ...

  2. PAT 1144 The Missing Number[简单]

    1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...

  3. [PAT] 1144 The Missing Number(20 分)

    1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...

  4. PAT_A1144#The Missing Number

    Source: PAT A1144 The Missing Number (20 分) Description: Given N integers, you are supposed to find ...

  5. PAT 甲级 1144 The Missing Number (20 分)(简单,最后一个测试点没过由于开的数组没必要大于N)

    1144 The Missing Number (20 分)   Given N integers, you are supposed to find the smallest positive in ...

  6. PAT(A) 1144 The Missing Number(C)统计

    题目链接:1144 The Missing Number (20 point(s)) Description Given N integers, you are supposed to find th ...

  7. Leetcode-268 Missing Number

    #268.  Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...

  8. Missing number

    Missing number 题目: Description There is a permutation without two numbers in it, and now you know wh ...

  9. 【LeetCode】268. Missing Number

    Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...

随机推荐

  1. Alter的用法(添加字段,删除字段,修改字段名)

    1.在表emp中新增字段sexy(性别) alter table emp add sexy varchar2(2); 新增多个字段cxx 和shoneworn alter table emp add  ...

  2. jQuery日期和时间插件(jquery-ui-timepicker-addon.js)中文破解版使用

    <html> <head> <title></title> <link type="text/css" href=" ...

  3. mysql中,执行delete语句时出现Lock wait timeout exceeded问题

    问题描述: 当我插入一条记录时,在调用save方法的时候出现了异常(记录重复了),导致了后面的commit语句不能执行了.这时我在数据库中删除重复记录时发现该表已经被锁上了.即出现错误.但过了一会再次 ...

  4. mac/linux install hg

    MAC OSX 10.9: sudo port -v install mercurial or easy_install mercurial

  5. psycopg2关于undefined symbol: lo_truncate64解决方法

    今天,在centos6.5下安装psycopg2,利用Python连接PostgreSQL数据库的时候,出现了一个undefined symbol: lo_truncate6的错误: django.c ...

  6. C++类继承中的构造函数和析构函数 调用顺序

    思想: 在C++的类继承中,构造函数不能被继承(C11中可以被继承,但仅仅是写起来方便,不是真正的继承) 建立对象时,首先调用基类的构造函数,然后在调用下一个派生类的构造函数,依次类推: 析构对象时, ...

  7. c语言 列出-终止系统进程

    #include <stdio.h> #include "stdafx.h" #include <Windows.h> #include <strin ...

  8. MEMS开关

    MEMS器件在射频比如无线通信上有很好的应用.RF MEMS谐振器和诱导器品质因子在微波上有大幅度提高.MEMS开关极大地改进了高频性能和降低了能耗.本篇概要介绍MEMS开关. 自从1979年彼特森( ...

  9. 【深夜急报,Win10下的Linux子系统之Bash】

    [在Windows下进行的编程人员,你真的需要学习下Linux] 手册:<Linux 命令手册(特洛伊版2.0)> 链接: https://pan.baidu.com/s/1skrVSvV ...

  10. Inno Setup 创建站点,创建虚拟目录

    原文 http://hi.baidu.com/0531_sunmiles/item/ce22554ab7d33d0be9350477 下面的这段代码是用Inno Setup 做安装包的时候创建IIS新 ...