【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 ...
随机推荐
- leetcode-mid-Linked list-94 Binary Tree Inorder Traversal
mycode 95% # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): ...
- SQL优化案例
8月9日对数据库进行优化 每日22:00开始跑批 经过优化,跑批时间缩短 逻辑读对比: 20180808逻辑读 20180809逻辑读 优化前后CPU负载对比:
- 用vuex实现购物车功能
效果图 展示目录结构 product组件(纯静态代码) cart组件(纯静态代码) info组件(纯静态代码) 完成以上的三个组件,现在要开始调用这些组件,在App.vue中调用 如果你的姿势正确的话 ...
- CDN分发
CDN 是Content Delivery Network,即内容分发网络. 未完待续..
- ABI与API的区别
ABI是定义二进制级别的,两个模块的接口 比如一个二进制模块想要调用另外一个二进制模块提供的功能,它需要知道怎样通过汇编语言(即机器指令)去调用,以及怎样传递相应的参数和返回值(通过寄存器还是栈内存, ...
- 使用NMAP端口扫描代码实现
Nmap是一个网络连接端扫描软件,用来扫描网上电脑开放的网络连接端.确定哪些服务运行在哪些连接端,并且推断计算机运行哪个操作系统(这是亦称 fingerprinting).其基本功能有三个,一是探测一 ...
- centos7下搭建Testlink环境详细过程
花了半天的时间终于搭建好了完整的Testlink环境,主要包括Mysql以及PHP的版本.未关闭防火墙.以及安装配置过程中遇到的一些问题.以下是详细的搭建过程. 一.工具准备 以下是我在搭建过程中用到 ...
- 【SQL Server复制】数据库复制:修改表结构、新增表、新增存储过程 会被复制到订阅服务器?
转自:https://www.cnblogs.com/happyday56/p/3849018.html 关键字:sql server复制 [SQL Server高可用性]数据库复制:修改表结构.新增 ...
- 使用Webpack的代码分离实现Vue懒加载(译文)
当一个Vue的项目体积变得十分庞大的时候,使用Webpack的代码分离功能将Vue Components,routes或Vuex的代码进行分离并按需加载,会极大的提高App的首屏加载速度. 在Vue的 ...
- Neo4j 不区分大小写的模糊查询匹配
问题:当图数据库中存储的节点的名字为英文时,就会遇到大小写不匹配问题. 使用不区分大小写的正则表示式可以解决以上问题. Cpyher的where语法里支持正则表达式 ,其语法为 : =~ &quo ...