【leetcode】First Missing Positive(hard) ☆
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.
思路:
又是一个不让排序,但是要得到跟排序有关信息的。想了半天,只能空间换时间,可是又只能用常量空间,这可难到我了。完全想不出来。放弃看答案。
发现,答案中其实是用了空间换时间的思想的,但是这里的空间直接就用了最开始的数组。
具体的思路是:从第一个数字开始,把正数换到按顺序排序的位置上。即1放到位置0,2放到位置1。直到当前位置的数字变成正确的数字。如果遇到小于0的数字或者大于元素个数的数字跳过,因为如果后面有对应位置的数字的话是会换过来的。
代码:
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
for(int i = ; i < nums.size(); ++i)
{
while(nums[i] > && nums[i] <= nums.size() && nums[nums[i] - ] != nums[i]) //其实这里不要nums[i] > 0 也可以 就是会慢一点
swap(nums[i], nums[nums[i] - ]); //交换数字 直到当前位置数字正确 或者 数字无法按序放在当前矩阵中
} int i;
for(i = ; i < nums.size(); ++i)
{
if(nums[i] != i + )
break;
}
return i + ;
}
};
【leetcode】First Missing Positive(hard) ☆的更多相关文章
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
- 【leetcode】First Missing Positive
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- 【LeetCode】163. Missing Range
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given a sorted integer array where the rang ...
- 【LeetCode】268. Missing Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 异或 日期 题目地址:https://leet ...
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- 【leetcode】1237. Find Positive Integer Solution for a Given Equation
题目如下: Given a function f(x, y) and a value z, return all positive integer pairs x and y where f(x,y ...
- 【LeetCode】163. Missing Ranges 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 【LeetCode】1060. Missing Element in Sorted Array 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 【leetcode】1228.Missing Number In Arithmetic Progression
题目如下: 解题思路:题目很简单.先对数组排序,根据最大值和最小值即可求出公差,然后遍历数组,计算相邻元素的差,如果差不等于公差,即表示数字缺失. 代码如下: class Solution(objec ...
随机推荐
- [译]Node.js Best Practices - Part 2
原文: https://blog.risingstack.com/node-js-best-practices-part-2/ 统一风格 在大团队开发JS应用, 创建一个风格指南是很有必要的. 推荐看 ...
- Access应用笔记<二>
关于access的应用笔记 20140822 基本完成access数据库的搭建,并且尝试了查重,不匹配项目查找,以及上传新数据等功能,表现良好. 记录一下目前研究出来的sql语句: 1)去除重复项 S ...
- svg技术(可缩放矢量图形)介绍
公司里面的产品用图表的地方也比较多,作为平台维护的我,收到几次需求提的建议中包括图表美化的功能,要炫,要3d,立体感,功能要强大等到:平台现有控件都是用的一个开源flash,我对flash虽然会一点但 ...
- 新手学JavaScript都要学什么?
要从事网页开发,不知从何下手? 会JavaScript语言但是不知道该如何进阶! 最好的方法就是不断地做网页.写代码,不断地看别人的代码,研究别人的代码! 用代码实践JS的每个技巧点:多听多看多问多写 ...
- 《深入浅出WPF》笔记四
1.WPF资源分布:数据库.资源文件.WPF对象资源.变量2.每个WPF的界面都具有一个名为Resources的属性,其类型为ResourceDictionary,以键值对的形式存储资源.3.检索资源 ...
- OpenCV imread读取图片,imshow展示图片,出现cv:Exception at memory location异常
问题如上.环境:VS2013. 代码如下: #include "stdafx.h" #include "opencv2\opencv.hpp" using na ...
- xcode 插件
http://www.cocoachina.com/ios/20160122/15080.html https://github.com/rickytan/RTImageAssets http://m ...
- 吉他笔记 solo 和弦 推弦 音程
十二平均律: 如下图所示: 第一行为唱名:do re mi fa so.... 第二行为音名:C #C D #D E F #F G #G A #A B C 第三行为D调对应的音名,即1 = D 第四行 ...
- BZOJ4439——[Swerc2015]Landscaping
0.题目: FJ有一块N*M的矩形田地,有两种地形高地(用'#'表示)和低地(用'.'表示) FJ需要对每一行田地从左到右完整开收割机走到头,再对每一列从上到下完整走到头,如下图所示 对于一个4* ...
- WebStorm设置手机测试服务器-局域网内其他设备访问
前端开发中,经常需要将做好的页面给其他同事预览或手机测试,之前一直用的第三方本地服务器usbwebserver,偶然了解到WebStorm内置服务器也可以满足此需求,来看看如何设置吧~~ 1.端口更改 ...