【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 ...
随机推荐
- 实现Java JTable的应用案例
代码如下 import Java.awt.Component; import java.awt.Dimension; import java.awt.FontMetrics; import javax ...
- 如何解决Android的SDK与ADT不匹配问题
win7/xp 下面安装Android虚拟机,更新SDK后,在Eclipse preference里指向android-sdk-windows时.出现 :This Android SDK requir ...
- 软件工程 speedsnail 冲刺9
20150313 完成任务:为周五检查做了相关准备,包括稳定性测试,分辨率测试: 遇到问题: 问题1 进入关卡的开始蜗牛会跳一段距离 解决1 没有解决 明日任务: 冲刺结束
- silverlight 获取路径 config
1.获取web.config配置内容: web.config default.aspx protected string InitParams { get; set; } InitParams = s ...
- php使用swoole实现一个简单的多人在线聊天群发
聊天逻辑的好多细节没有实现,只实现群发. php代码: $serv = new swoole_websocket_server("127.0.0.1",3999); //服务的基本 ...
- BT9034: 仅 IE 和 Opera 支持 HTMLFrameElement 和 HTMLIFrameElement 的 document 属性
标准参考 根据 DOM-2 中的描述,HTMLFrameElement 和 HTMLIFrameElement 都没有 'document' 属性. 关于 HTMLFrameElement 对象的详细 ...
- Python 有哪些优点?为何要学Python?
1. 支持OOP编程 从根本上讲Python仍是一种面向对象的语言,支持多态.继承等高级概念,在Python里使用OOP十分容易 没有C++.Java那样复杂,但不必做Python下OOp高手,够 ...
- C# WPF使用ZXing生成二维码ImageSource
介绍: 如果需要实在WPF窗体程序中现类似如下的二维码图片生成功能,可以通过本文的方法实现 添加步骤: 1.在http://zxingnet.codeplex.com/站点上下载ZXing .Net的 ...
- 微软CRM解决医药企业串货之痛
没有准确.及时的流向数据统计和分析,医药企业营销部门就无法有效管理串货泛滥问题,串货会造成渠道无利可赚,挫伤渠道的积极性,产品无人愿意卖,最终伤害的还是医药企业. 医药企业营销发展的不同阶段对串货的态 ...
- EXCEL处理数据小技巧
1.EXCEL 中如何获得4*8这个数字中,*号前面的数字 输入 =LEFT(AC2,FIND("*",AC2)-1) 获得*右边的数字 输入 =RIGHT(AC2,FIND(& ...