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. escape和unescape给字符串编码

    var before = "\xxx\xxx" var after = escape(before); var after2 = unescape(after );

  2. word 中巧妙添加分隔线

  3. union以及一些扩展

    select name,age from Students where Age<3unionselect name ,age from Students where Age >4--两个结 ...

  4. NSDictionary初始化,使用@{}方法,插入nil时会报空指针异常

    由于今天在NSDictionary初始化的时候出现Crash异常,故记录一下,避免下次再犯. 在Objective-C中,NSDictionary初始化的方法有很多种 方法1: [NSDictiona ...

  5. (转)MVC语法-@helpers和@functions(Razor内定义函数)

    (转)MVC语法-@helpers和@functions(Razor内定义函数) 转自:http://www.mikesdotnetting.com/Article/173/The-Differenc ...

  6. Struts2 工作流程

    Struts2使用了WebWork的设计核心(XWork),在内部使用拦截器处理用户请求,从而允许用户业务逻辑控制器和ServletAPI分离.Struts2内部是一个MVC架构,Struts2 的核 ...

  7. MySQL计数器表的设计

    如果应用在表中保存计数器,则在更新计数器时可能碰到并发问题.计数器表在web应用中非常常见.可以用这个表缓存一个用户的朋友书.文件下载次数等.创建一张独立的表存储计数器是一种非常好的做法,这样可以使计 ...

  8. Eddy's digital Roots(九余数定理)

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  9. alsa音频驱动科普第一课

    做linux音频编程对alsa应该不陌生. 但是对于刚接触这块技术的同学来说是一件困难的事情.原因在于:网上关于alsa的资料太少了,特别国内的资料更是大部分重复.对于初学者来说特别苦恼. 由于笔者经 ...

  10. python操作 redis-list

    #!/usr/bin/python #!coding: utf-8 import redis if __name__=="__main__": try: conn=redis.St ...