leetcode problem 41 -- First Missing Positive
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,2,4,5的第一个缺失的正整数是3.要求时间复杂度为o(n) 空间复杂度为o(1)
思路:
第一种是排序,时间复杂度为O(nlgn) 不行
第二种是hash查找 对每一个数字都用额外空间找到正确的位置. 然后一次遍历找到第一个缺失的正整数. 时间复杂度为O(n) 但是空间复杂度为O(n) 也不符合题目要求
第三种直接在输入数组上进行交换操作, 使得每个数字通过交换能在正确的位置上.如3,1,7,2 这组数字第一次交换后得到7,1,3,2 数字3放置在了正确的位置.
这种时间复杂度为O(n) 而且只需要常数辅助空间. 但我认为这种方法依然不复合题目要求,因为它破坏了原始数组. 它其实也类似于hash, 需要O(n)的空间,只不过这O(n)的空间借用了原始输入数组上的.
目前我还没找到即不破坏原始数组,空间复杂度又为O(1)的方法
代码1(hash法):
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
if (nums.size() == )
return ;
auto it_max = std::max_element(nums.begin(), nums.end());
auto it_min = std::min_element(nums.begin(), nums.end());
vector<bool> vec(*it_max - *it_min, false);
for (auto elem : nums)
vec[elem - *it_min] = true;
for (auto it = ; it <= *it_max; ++it) {
if (it < *it_min || (it > && !vec[it - *it_min]))
return it;
}
return *it_max > ? *it_max + : ;
}
private:
};
代码2(直接在原数组上进行交换, 空间代价为o(1))
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
if (nums.empty())
return ;
auto begin = getFirstPositivePos(nums);
if (*begin < )
return ;
for (auto it = begin; it != nums.end(); ) {
auto realPos = begin + *it - ;
if (realPos < nums.end() && realPos != it && *realPos != *it)
iter_swap(it, realPos);
else
++it;
}
int index = ;
for (auto it = begin; it != nums.end(); ++it, index++) {
if (index != *it)
return index;
}
return index;
}
private:
vector<int>::iterator getFirstPositivePos(vector<int>& nums) {
auto first = nums.begin();
auto last = nums.end()-;
while (true) {
while (first < last && *first <= )
++first;
while (first < last && *last > )
--last;
if (first < last)
iter_swap(first, last);
else
break;
}
return first;
}
};
leetcode problem 41 -- First Missing Positive的更多相关文章
- [Leetcode][Python]41: First Missing Positive
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...
- LeetCode题解41.First Missing Positive
41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. ...
- 【一天一道LeetCode】#41. First Missing Positive
一天一道LeetCode系列 (一)题目 Given an unsorted integer array, find the first missing positive integer. For e ...
- LeetCode OJ 41. First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- 【LeetCode】41. First Missing Positive (3 solutions)
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- 【leetcode】41. First Missing Positive
题目如下: 解题思路:这题看起来和[leetcode]448. Find All Numbers Disappeared in an Array很相似,但是有几点不同:一是本题的输入存在负数,二是没有 ...
- LeetCode - 41. First Missing Positive
41. First Missing Positive Problem's Link ---------------------------------------------------------- ...
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
- 乘风破浪:LeetCode真题_041_First Missing Positive
乘风破浪:LeetCode真题_041_First Missing Positive 一.前言 这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制. 二.First Missing Posi ...
随机推荐
- squid3.0 隐藏 hearder 设置
原文地址: http://www.wenzizone.cn/?p=311 squid2.6可以使用header_access来控制显示与隐藏http的header信息,但是在squid3.0版本中这个 ...
- Remove “System Program Problem Detected” Messages From Ubuntu
One of my Ubuntu systems would pop up the following message multiple times after logging in: System ...
- ArcGIS教程:加权叠加
摘要 使用经常使用測量比例叠加多个栅格数据,并依据各栅格数据的重要性分配权重. 插图 插图中,两个输入栅格已又一次分类为 1 至 3 三种公共測量级别.为每一个栅格均分配了一个影响百分比.这些像元值与 ...
- spring关于“transactionAttributes”的相关配置
spring关于"transactionAttributes"的相关配置 <bean id="baseTransactionProxy" class=&q ...
- uploadify上传文件Firefox浏览器上传失败解决方法
近期做文件上传使用到了uploadify 可是出现了各种奇葩的问题.并且针对各个不同浏览器问题不同 在Firefox中.非常坑爹的是.每次上传就丢失session值,可是我的系统在登录.保存文件文件夹 ...
- Android 设置横屏或竖屏
方法一:在AndroidManifest.xml中配置 如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的AndroidManifest.xml中找到你所指定的activity中加上androi ...
- Android手机上判断网络运营商
我们想获取手机的运营商信息.通常都会去调用系统的TelephonyManager类的取数据.但是很多时候可能取不到卡的信息(例如双卡手机和 一些特殊卡),这样就区别不了运营商了.但是有时候我们的需求要 ...
- android学习日记03--常用控件button/imagebutton
常用控件 控件是对数据和方法的封装.控件可以有自己的属性和方法.属性是控件数据的简单访问者.方法则是控件的一些简单而可见的功能.所有控件都是继承View类 介绍android原生提供几种常用的控件bu ...
- 3D分析之3D要素工具箱(转)
来自:http://blog.csdn.net/kikitamoon/article/details/8193764 整理有关 ArcGIS 10.1 3D分析工具箱中,3D Feature 工具箱中 ...
- 可扩展的listview--Expandablelistview
layout.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...