题目:

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#局域网桌面共享软件制作(三)

    到周末了,继续做这个桌面共享软件,下面是前两篇的链接, 链接 C#局域网桌面共享软件制作(一) 链接 C#局域网桌面共享软件制作(二) 通过对图片进行压缩以后,每张图片大小38K左右(win7/102 ...

  2. HTML5 对于手机页面长按会粘贴复制的禁用 (解决方案)

    解决方案: 直接在CSS 文件中添加下面的代码,就可以实现了在手机端禁止粘贴复制的功能: *{    -webkit-touch-callout:none;  /*系统默认菜单被禁用*/    -we ...

  3. MongoDb Replica Set中使用的地址

    Unable to connect to a member of the replica set matching the read preference Primary 今天尝试使用MongoDB ...

  4. 什么是Ajax无刷新技术?

    浏览器实例化一个Ajax对象,这个对象发送一个HTTP请求,并且携带一定的参数,传输到后台.后台服务器接收这些参数,同时过滤一下传过来的参数,做出逻辑判断.如果需要数据库操作参与,就要取出数据,格式化 ...

  5. Discuz X3.2 分区 gid 完美伪静态方法 Apache/Nginx

    Discuz 官方给出的伪静态规则并不完整,只实现了部分的伪静态设置及规则,分区 gid 仍然是 forum.php?gid=xxx 的形式,对于有强迫症的我是无法忍受的,下面给出分区 gid 的伪静 ...

  6. [Linux] Ubuntu Server 12.04 LTS 平台上搭建WordPress(Nginx+MySQL+PHP) Part IV

    接下来我们去下载 WorePress 用最新的 3.7.1 下载地址是:http://cn.wordpress.org/wordpress-3.7.1-zh_CN.zip 我们先建立一个文件夹 /va ...

  7. PHP判断用户所在国家并跳转对应的目录

    <?php // 淘宝API查询国家代码 $url = "http://ip.taobao.com/service/getIpInfo.php?ip=".get_client ...

  8. xmpp push篇一 广播消息

    ---广播给所有人--- 1. 登录xmpp admin 账户 2. sendpacket <message to="pandans.com(域名)" > <bo ...

  9. php中的占位符

    1.?这种形式传值,注意是数组! 2.:name的形式.

  10. 集合删数 (vijos 1545) 题解

    [问题描述] 一个集合有如下元素:1是集合元素:若P是集合的元素,则2 * P +1,4*P+5也是集合的元素,取出此集合中最小的K个元素,按从小到大的顺序组合成一个多位数,现要求从中删除M个数位上的 ...