题目:

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.

代码:

class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
const int len = nums.size();
for ( int i = ; i < len; ++i )
{
while ( nums[i]!=i+ )
{
if ( nums[i]>len || nums[i]< || nums[i]==nums[nums[i]-] ) break;
std::swap(nums[i], nums[nums[i]-]);
}
}
for ( int i = ; i < len; ++i )
{
if ( nums[i]!=i+)
return i+;
}
return len+;
}
};

tips:

通过此题学习了桶排序(bucket sort)

桶排序原理讲解 http://bubkoo.com/2014/01/15/sort-algorithm/bucket-sort/

桶排序演示动画 http://www.cs.usfca.edu/~galles/visualization/BucketSort.html

大神的解答 http://fisherlei.blogspot.sg/2012/12/leetcode-first-missing-positive.html

自己coding的时候思考过程如下:

1. 此题设计的是最简单的桶,利用nums[i]存放i+1

2. 如果某个位置上nums[i]不等于i+1,则就swap(nums[i], nums[nums[i]-1])

  为什么是这样的?因为nums[i]=i+1→nums[i]-1=i→nums[nums[i]-1]=i,简单说就是把nums[i]这个值放到它该放到的桶里面。

3. 紧接着考虑2中swap能成每次都立么?显然不能:nums[i]>len nums[i]<1这两个条件出现时已经超出了nums数组量程。显然是不行的。

还有一种隐蔽的情况也是不能swap的,比如[1,1]会陷入死循环,因此在swap之前还需要判断nums[i] != nums[nums[i]-1]。

4. 把1~3的过程走完,再遍历数组nums,判断nums[i]!=i+1第一次出现的位置。

5. 如果4中把nums都走过一遍,也没有发现缺失的,则证明缺失的是len+1。

=============================================

第二次过这道题,直接记住思路了,默写了一遍。

class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
for ( int i=; i<nums.size(); ++i )
{
while ( nums[i]!=i+ )
{
if ( nums[i]>nums.size() || nums[i]< || nums[i]==nums[nums[i]-] ) break;
swap(nums[i],nums[nums[i]-]);
}
}
for ( int i=; i<nums.size(); ++i )
{
if ( nums[i]!=i+ ) { return i+; }
}
return nums.size()+;
}
};

【First Missing Positive】cpp的更多相关文章

  1. hdu 4740【模拟+深搜】.cpp

    题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...

  2. 【Combination Sum II 】cpp

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  3. 【Search Insert Position 】cpp

    题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...

  4. 【Insertion Sorted List】cpp

    题目: Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct L ...

  5. 【Merge Sorted Array】cpp

    题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...

  6. 【Path Sum II】cpp

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  7. 【Longest Common Prefix】cpp

    题目: Write a function to find the longest common prefix string amongst an array of strings. 代码: class ...

  8. 【 Regular Expression Matching 】cpp

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  9. 【Longest Palindromic Substring】cpp

    题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

随机推荐

  1. 小菜的系统框架界面设计-灰姑娘到白雪公主的蜕变(工具条OutLookBar)

    灰姑娘本身也有自已的优点,但是却可能因为外貌不讨人喜欢,要变成白雪公主却需要有很多勇气和决心去改变自已: 有一颗善良的心 讨人喜爱的外貌   --蜕变-->  我这里讲的是一个工具条的蜕变过程, ...

  2. [原]Django调试工具--django-debug-toolbar

    请摒弃简单粗暴的print --马云 我比较习惯在windows中安装pycharm开发,项目部署在虚拟机中,在本地浏览器中查看效果,这种方式在调试上会有点麻烦,django-debug-toolba ...

  3. php不使用插件导出excel

    php不使用插件导出excel的简单方法,首先获取需要导出的数据的数组,数组的格式在下面. 之后就是定义文件名称和需要导出的excel的样式,最后就是循环数组,输出数据了 代码: $filename= ...

  4. IntelliJ IDEA 13破解(JRebel 5.6.3a破解)

    首先安装IntelliJ 13,记得要下载Ultimate Edition版本,不然就不需要破解了.. 安装到本地,然后进行一些配置(这一步可以不要,但是考虑到以后换系统可以省事,推荐做) 打开{in ...

  5. Yii框架中使用PHPExcel导出Excel文件

    最近在研究PHP的Yii框架,很喜欢,碰到导出Excel的问题,研究了一下,就有了下面的方法: 1.首先在config\main.php中添加对PHPExcel的引用,我的方式是这样: 1 2 3 4 ...

  6. Linux之uboot分析与移植20160601

    说一下uboot分析与移植: 1.下载.建立source insight工程.编译.烧写.如果无运行分析原因 tar xjf u-boot-2012.04.01.tar.bz2 cd u-boot-2 ...

  7. 用O(1)的时间复杂度,找到栈和队列中的最小(大)值

    最近刷剑指offer,看到两道编程题,考察在O(1)的复杂度内,找出最值. 觉得很有意思,很有借鉴意义,故记录在此. 需要注意的是,这里所说的O(1) 有个前提, 就是已经通过某种容器的存储方式进行初 ...

  8. SublimeText插件Emmet的自定义模板

    在前端界,作为快速生成代码的Emmet插件相当给力.最近在学bootstrap,需要频繁生成html头文件,我就想着自定义模板.国内只有基础教程,只好自己读英文文档了. Emmet国内基础教程地址: ...

  9. (转)python文件操作 seek(),tell()

    seek():移动文件读取指针到指定位置 tell():返回文件读取指针的位置 seek()的三种模式: (1)f.seek(p,0)  移动当文件第p个字节处,绝对位置 (2)f.seek(p,1) ...

  10. VC下的人人对弈五子棋(dos)

    #include"stdio.h"#include"stdlib.h"#include"string.h"#include "io ...