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. 百度搜索附近加盟店等基于LBS云搜索功能的实现

    一.注册百度账号,进入开发者平台 创建应用并获取ak 地址如下 http://lbsyun.baidu.com/apiconsole/key/update?app-id=7546025 ok获取到了. ...

  2. My97DatePicker使用的问题

    我在iframe中使用My97DatePicker时,发现第一次点击左边的菜单时,在右边的页面可以弹出日期框: 当我第二次点击菜单时,右边的日期文本框却弹出了页面的内容,而不是日期选择框: 首先怀疑是 ...

  3. Redis 作为缓存服务器的配置

    随着redis的发展,越来越多的架构用它取代了memcached作为缓存服务器的角色,它有几个很突出的特点:1. 除了Hash,还提供了Sorted Set, List等数据结构2. 可以持久化到磁盘 ...

  4. RecyclerView不同类型Item的展示

    代码如下: public class AccessoiresAdapter extends RecyclerView.Adapter { final int VIEW_TYPE_ACCESSORY = ...

  5. CentOS6.5下Mysql数据库的安装与配置

    一.mysql简介 说到数据库,我们大多想到的是关系型数据库,比如mysql.oracle.sqlserver等等,这些数据库软件在windows上安装都非常的方便,在Linux上如果要安装数据库,咱 ...

  6. Windows -DOS 下Yii创建应用及出错处理

    Win7下,以管理员身份运行cmd.exe 本例是在wamp环境下:切换到d盘,再切换到framework目录.............1--C:\windows\system32>d: 2-- ...

  7. redis怎么动态添加内存,动态配置,无需重启。

    在redis的使用过程中,有时候需要急需修改redis的配置,比如在业务运行的情况下,内存不够怎么办,这时要么赶紧删除无用的内存,要么扩展内存.如果有无用的内容可删除那么所有问题都已经解决.如果内容都 ...

  8. Immediate Decodability(字典树)

    Immediate Decodability Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  9. 转的git

    原文链接:http://blog.csdn.NET/dengjianqiang2011/article/details/9260435 如果输入$ Git remote add origin git@ ...

  10. 在C#中调用Win32函数EnumWindows枚举所有窗口。

    原文 http://www.cnblogs.com/mfm11111/archive/2009/06/30/1514322.html 开发旺旺群发软件,难点及重要技术点分析(一) 一.        ...