【First Missing Positive】cpp
题目:
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的更多相关文章
- hdu 4740【模拟+深搜】.cpp
题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...
- 【Combination Sum II 】cpp
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 【Search Insert Position 】cpp
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
- 【Insertion Sorted List】cpp
题目: Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct L ...
- 【Merge Sorted Array】cpp
题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...
- 【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 ...
- 【Longest Common Prefix】cpp
题目: Write a function to find the longest common prefix string amongst an array of strings. 代码: class ...
- 【 Regular Expression Matching 】cpp
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- 【Longest Palindromic Substring】cpp
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
随机推荐
- CentOS学习笔记—启动、ROOT密码
启动流程一览 简单来说,系统启动的经过可以汇整成底下的流程的: 加载 BIOS 的硬件资讯与进行自我测试,并依据配置取得第一个可启动的装置: 读取并运行第一个启动装置内 MBR 的 boot Load ...
- 那么如何添加网站favicon.ico图标
1. 获得一个favicon.ico的图标,大小为16px×16px最为合适 2. 将制作好的图标文件Favicon.ico上传到网站的根目录: 3. 在首页文件的html代码的头部中加入如下代码: ...
- 如何用asp.net MVC框架、highChart库从sql server数据库获取数据动态生成柱状图
如何用asp.net MVC框架.highChart库从sql server数据库获取数据动态生成柱状图?效果大概是这样的,如图: 请问大侠这个这么实现呢?
- ASP.NET的错误处理机制之二(实例log4net)
一.log4net下载:http://logging.apache.org/log4net/download_log4net.cgi 二.web.config配置如下: <?xml versio ...
- php 安装redis扩展
大家可以去http://code.google.com/p/redis/downloads/list这个地址找最近的下载wget http://redis.googlecode.com/files/r ...
- cocos run -p android报错 BUILD FAILED ..\ant\build.xml:892
使用编译指令生成apk文件时,出现这个错误,是因为重复引用了..\YourGame\cocos2d\cocos\platform\android\java\bin\classes.jar文件. 为什么 ...
- Oracle 10g 之自动收集统计信息
从10g开始,Oracle在建库后就默认创建了一个名为GATHER_STATS_JOB的定时任务,用于自动收集CBO的统计信息.这个自动任务默认情况下在工作日晚上10:00-6:00和周末全天开启. ...
- 比较C++中的4种类型转换方式
C++的四种cast操作符的区别并非我的原创-------------------------------------------from:http://blog.csdn.net/hrbeuwhw/ ...
- android 中单选和复选框监听操作
单选按钮RadioGroup.复选框CheckBox都有OnCheckedChangeListener事件,我们一起了解一下. package com.genwoxue.oncheckedchange ...
- hdu 1195 Open the Lock
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1195 Open the Lock Description Now an emergent task f ...