题目如下:

解题思路:这题看起来和【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. DelayQueue 源码分析

    DelayQueue DelayQueue 是基于 PriorityQueue 实现的线程安全的无界优先级阻塞队列, 队列的头部元素必须在超时后才能移除,元素必须实现 Delayed 接口. 创建实例 ...

  2. 用Vue来实现音乐播放器(五):路由配置+顶部导航栏组件开发

    路由配置 在router文件夹下的index.js中配置路由 import Vue from 'vue' import Router from 'vue-router'//配置路由前先引入组件impo ...

  3. Swiper轮播手动后不动

    最近项目首页轮播图用了Swiper轮播,今天突然发现轮播图动画初始正常但是手动换过之后就不动了,解决方法有两种,具体根据采用的情况为准: 1.autoplayDisableOnInteraction: ...

  4. Caffe深度学习计算框架

    Caffe | Deep Learning Framework是一个清晰而高效的深度学习框架,其作者是博士毕业于UC Berkeley的 Yangqing Jia,目前在Google工作.Caffe是 ...

  5. 【CTS】几个serialno失败项

    [问题结论] [Common]SN配置项的问题,只可以'数字与大小写字母' 将配置SN改为字母数字组合,测试全部pass [问题描述] CTS三条失败项 run cts -m CtsTelephony ...

  6. Ngix 配置与部署(wsgi,uwsgi,uWSGI)

    1. WSGI 是一种协议接口,他是描述web服务器如何与web应用程序(Django ,Flask ) 通讯的规范. 2. uwsgi 与WSGI协议一样,是uWSGI服务器的独占协议,用于定义传输 ...

  7. discover面试

    电话面 英语 20191009 面试我的是一个小姐姐,她是数据挖掘分析师,声音很甜很好听.全程英文 (1)please introduce yourself in two minutes (2)我对你 ...

  8. 如何实现动态水球图 --》 echars结合echarts-liquidfill实现

    1)项目中作为项目依赖,安装到项目当中(注意必须要结合echars) npm install echarts vue-echarts --save npm install echarts-liquid ...

  9. Java实验报告(一)&&第三周学习总结

    实验报告(一) 1. 打印输出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其中各位数字立方和等于该数本身.例如,153是一个“水仙花数”. 源代码: public class Main { p ...

  10. [Python3 练习] 002 温度转换2

    题目:温度转换 II (1) 描述 温度的刻画有两个不同体系:摄氏度 (Celsius) 和华氏度 (Fabrenheit) 请编写程序将用户输入的华氏度转换为摄氏度,或将输入的摄氏度转换为华氏度 转 ...