题目如下:

解题思路:这题看起来和【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的更多相关文章

  1. 【LeetCode】41. First Missing Positive (3 solutions)

    First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...

  2. 【一天一道LeetCode】#41. First Missing Positive

    一天一道LeetCode系列 (一)题目 Given an unsorted integer array, find the first missing positive integer. For e ...

  3. [Leetcode][Python]41: First Missing Positive

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...

  4. LeetCode题解41.First Missing Positive

    41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. ...

  5. 【LeetCode题意分析&解答】41. First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  6. LeetCode OJ 41. First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  7. leetcode problem 41 -- First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  8. 【LEETCODE】41、905. Sort Array By Parity

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  9. 【leetcode】 First Missing Positive

    [LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...

随机推荐

  1. flex 数字上标

    以A的3次方为例,我们输入以下代码: /** * 部分代码参考Adobe文档: * http://help.adobe.com/zh_CN/AS3LCR/Flash_10.0/flash/text/e ...

  2. python抽象篇:面向对象基础

    1.面向对象概述 面向过程编程:根据操作数据的函数或语句块来设计程序的. 函数式编程:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象编程:数据和功能结合起来,用称为对象的东西包 ...

  3. 在自定义组件中获取spring底层组件

    要想在自定义组件中获取spring底层的各种组件,只需让自定义组件实现一系列接口即可,这些接口都是Aware的子接口.常见的有: 1. ApplicationContextAware——用于获取IOC ...

  4. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_07 缓冲流_2_BufferedOutputStream_字节缓冲

    子类 继承父类,这些方法都可以使用 必须写上flush,刷新数据数据才能写入到文件内

  5. Java ——Character 类

    本节重点思维导图 Character 类用于对单个字符进行操作. Character 类在对象中包装一个基本类型 char 的值 char ch = 'a'; // Unicode 字符表示形式 ch ...

  6. Windows操作系统命令整理-Win7

    注册表(Windows95以后Windows系统版本,windows server中regedit和regedit32合并为一个新的编辑器,名称仍然是regedit) HKEY_LOCAL_MACHI ...

  7. debian下使用shell脚本时出现了 declare:not found 解决方法

    问题:出现declare:not found的提示 解决:原来,UBUNTU用的是dash(后来证明这个其实这个不是错误的原因:从#!/bin/bash到#!/bin/dash,依旧无法运行,在这写出 ...

  8. 20191003 尚硅谷Spring Cloud教学视频

    视频信息 视频日期:2018-4-19 讲师:尚硅谷周阳 Spring Cloud版本:Dalston.RELEASE 当前版本:Greenwich SR3 微服务.微服务架构.Spring Clou ...

  9. sql中的isnull

    ISNULL 使用指定的替换值替换 NULL. 语法ISNULL ( check_expression , replacement_value ) 参数check_expression 将被检查是否为 ...

  10. 【洛谷p1158】导弹拦截

    这道题是个有想法的枚举qwq 导弹拦截[题目链接] 注意:此导弹拦截非彼导弹拦截p1020 导弹拦截 一道题是1999年的,然后我们现在要写的是经过11年韬光养晦之后的导弹拦截 SOLUTION: 要 ...