题目:

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. c++强制类型转换(static_cast,const_cast,dynamic_cast,reinterpret_cast)

    static_cast <typeid>(exdlvssion) static_cast 很像 C 语言中的旧式类型转换.它能进行基础类型之间的转换,也能将带有可被单参调用的构造函数或用户 ...

  2. Interpreter

    #include <string> #include <iostream> #include <stack> using namespace std; stack& ...

  3. HTML新特性之一----canvas

    <canvas id="me"></canvas>//申请一个canvas标签    <script>                var c ...

  4. Javascript 模块模式

    模块模式(Module Pattern)提供了一种代码封装的方式,可以优雅地创建非耦合的代码块. 它是利用即时函数为对象创建私有变量和特权方法.严格来说,Javascript中没有私有成员的概念,所有 ...

  5. 查看软、硬raid信息的方法

    软件raid:只能通过Linux系统本身来查看cat /proc/mdstat可以看到raid级别,状态等信息. 硬件raid:最佳的办法是通过已安装的raid厂商的管理工具来查看,有cmdline, ...

  6. 软件工程 speedsnail 第二次冲刺2

    20150519 完成任务:划线第二天,能画出一条直黄线: 遇到问题: 问题1 划线的代码和移动的setcontentview冲突,无法同时显示 解决1 没有解决 明日任务: 线与移动共存

  7. C# Datetime类常用技巧

    C#类常用技巧 //今天DateTime.Now.Date.ToShortDateString();//昨天,也就是今天的日期减一DateTime.Now.AddDays(-1).ToShortDat ...

  8. sqoop的eval工具

    eval的作用:Evaluate a SQL statement and display the results,也就是说eval像是一个数据库的客户端工具. 一.使用eval来查询表 $ sqoop ...

  9. 分析MapReduce执行过程

    分析MapReduce执行过程 MapReduce运行的时候,会通过Mapper运行的任务读取HDFS中的数据文件,然后调用自己的方法,处理数据,最后输出. Reducer任务会接收Mapper任务输 ...

  10. Android中的显示单位

    px (pixels)像素 一般HVGA代表320x480像素,这个用的比较多. dip或dp (device independent pixels)设备独立像素 这个和设备硬件有关,一般为了支持WV ...