【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 ...
随机推荐
- Octal Fractions
Octal Fractions Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 149 Solved: 98 Description Fractions ...
- Linux 开机启动方式设置 inittab 详解,开机直接进入“命令行”模式
Linux下的 /etc/inittab 中的英文解释: This file describes how the INIT process should set up the system in a ...
- Jquery中的事件和动画
在学习Jquery中的过程中我们绝大部分都用到了事件的操作,也可以说事件是Jquery中必不可少的一部分,我们常见的一些事件有单击事件,鼠标事件,键盘事件等等.在Jquery中的学习中为了能使让页面以 ...
- APPCAN IDE中安装emmet插件
1.首先打开APPCAN IDE 2.帮助(help)-安装新软件(install New sofaWare) 3.打开Install窗口,点击 Add,在Add Repository窗口中,Name ...
- mongodb university week4
1.index Creation,background 如果在foreground运行index,会阻塞其他writer,如果background运行,会比较慢,但不会阻塞其他writer,可以并发写 ...
- 更新引用google的cdn外部jQuery核心库JS文件
jquery-2.0.3 注!不再支持IE 6/7/8 直接引用地址: <script src="http://ajax.googleapis.com/ajax/libs/jque ...
- Android开发App工程结构搭建
本文算是一篇漫谈,谈一谈关于android开发中工程初始化的时候如何在初期我们就能搭建一个好的架构. 关于android架构,因为手机的限制,目前我觉得也确实没什么大谈特谈的,但是从开发的角 ...
- tab切换,滑动门
<SCRIPT type=text/javascript> jQuery(document).ready(function () { changediv([" ...
- 对C++虚函数、虚函数表的简单理解
一.虚函数的作用 以一个通用的图形类来了解虚函数的定义,代码如下: #include "stdafx.h" #include <iostream> using name ...
- BZOJ 4580: [Usaco2016 Open]248
Description 一个序列,每次可以把相邻的两个数合为一个,价值+1,求最后的最大价值. Sol 区间DP. \(f[i][j]\) 表示 \(i-j\) 中合成一个数字为多少,转移就是枚举断点 ...