【leetcode】41. First Missing Positive
题目如下:

解题思路:这题看起来和【leetcode】448. Find All Numbers Disappeared in an Array很相似,但是有几点不同:一是本题的输入存在负数,二是没有约定输入元素的最大值。那么,怎么可以把本题转换成448题的场景呢?首先,我们可以求出输入数组nums中所有正整数的数量,记为p,那么显然能得出这个结论:1 <=answer < p+1。然后,我们可以通过交换把所有值不在这个区间内的元素值移动到数组的后半部分。记nums前半部分[0:length]为符合answer取值区间的元素,既然已经得到了这个区间,那么就可以模仿448题的方法再次交换,使得元素的值和下标匹配。
代码如下:
class Solution(object):
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
positiveCount = 0
minv = float('inf')
for i in nums:
if i <= 0:
continue
minv = min(minv,i)
positiveCount += 1
if minv < 1:
return 1
endInx = len(nums)-1
for i in xrange(len(nums)):
if nums[i] <= 0 or nums[i] > positiveCount:
for j in xrange(endInx,i,-1):
if nums[j] > 0 and nums[j] <= positiveCount:
t = nums[i]
nums[i] = nums[j]
nums[j] = t
endInx = j
break
# nums subarray is nums[:endInx]
length = len(nums)
for i in xrange(len(nums)):
if nums[i] > positiveCount or nums[i] < 0:
length = i
break
#nums[:length] 这个子集保存的是所有符合answer的元素
i = 0
while i < length:
if nums[i] >= length:
i += 1
pass
elif nums[i]-1 != i and nums[nums[i]-1] != nums[i]: # 记 nums = [1,1,2] ,第二个1是否要移动到下标为0的位置,要判断下标为0的元素是否已经和下标匹配
src = nums[i]
dest = nums[nums[i]-1]
nums[nums[i]-1] = src
nums[i] = dest
else:
i += 1 #print nums[:length]
for i in xrange(length):
if i+1 != nums[i]:
return i+1
return length+1
【leetcode】41. First Missing Positive的更多相关文章
- 【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系列 (一)题目 Given an unsorted integer array, find the first missing positive integer. For e ...
- [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
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- LeetCode OJ 41. First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- leetcode problem 41 -- First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- 【LEETCODE】41、905. Sort Array By Parity
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
随机推荐
- optistruct线性求解一次二次单元应力位移比较
通过分析比较10mm.5mm.3mm.1mm的网格模型, 网格越细密: 位移与应力均趋于恒定值(收敛): 一次与二次单元的应力区域一致: 一次与二次单元的位移相差11.3%,一次单元的位移小. 所用的 ...
- inode节点用尽处理
linux inode已满解决方法 原文 今天login server的一个网站,发现login后没有生成session.根据以往经验,一般是空间已满导致session文件生成失败. df -h Fi ...
- ORACLE DG在线日志修改
ORACLE DG在线日志修改 SQL>select SEQUENCE#,first_time,next_time,APPLIED, THREAD# from v$archived_log or ...
- 一个DRF框架的小案例
第一步:安装DRF DRF需要以下依赖: Python (2.7, 3.2, 3.3, 3.4, 3.5, 3.6) Django (1.10, 1.11, 2.0) DRF是以Django扩展应用的 ...
- C printf格式化输出 的跨平台
printf()在不同的系统上,占位符的可能有不同的写法,如: %ld:long int, 32位平台4 bytes %lld: long long int, 32位平台8 bytes 可以考虑使用 ...
- 测开之路一百零六:bootstrap布局
可以在html的head里面加一些说明 <meta http-equiv="X-UA-Compatible" content="IE=edge">& ...
- 《Using Databases with Python》Week1 Object Oriented Python 课堂笔记
Coursera课程<Using Databases with Python> 密歇根大学 Charles Severance Week1 Object Oriented Python U ...
- bootstrap-select控件全选,全不选,查询功能实现
先引入先在你的页面引入 bootstrap-select.css 和 bootstrap-select.js <link href="~/Content/plugins/bootstr ...
- 剑指offer--day07
1.1 题目:反转链表:输入一个链表,反转链表后,输出新链表的表头. 1.2 思路:这道题,我们要做到的是反转链表,我们的思路是将前一个节点与后一个节点断开,然后让后一个节点指向前一个节点,这个过程就 ...
- java锁的概念
在学习或者使用Java的过程中进程会遇到各种各样的锁的概念:公平锁.非公平锁.自旋锁.可重入锁.偏向锁.轻量级锁.重量级锁.读写锁.互斥锁等待.这里整理了Java中的各种锁,若有不足之处希望大家在下方 ...