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开始查找,找到了1再找2,这样一直找到缺失的第一个正整数。比如[1,2,0] return 3,[3,4,-1,1] return 2. 要求时间复杂度O(n) ,空间复杂度为常数。

解答:

  我们要注意到这样一个事实,数组的下标是有标记意义的。所以我们可以把数字放到相应的下标下面,这样理想情况下所有的正数都能一一对应到0~N-1的下标中,这里面可以使用交换来实现。这样一次循环之后,正整数i应该交换到了i-1下标对应的元素中。然后在进行一次循环查找第一个不符合的元素输出即可。

AC代码:

class Solution(object):
def firstMissingPositive(self, nums):
i, n = 0, len(nums)
while i < n:
if nums[i] > 0 and nums[i] <= n and nums[i] != nums[nums[i] - 1]:
# swap
temp = nums[i]
nums[i] = nums[nums[i] - 1]
nums[temp - 1] = temp
else:
i += 1
for i, v in enumerate(nums):
if v != i + 1:
return i + 1
return n + 1

【LeetCode题意分析&解答】41. First Missing Positive的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. 【LeetCode题意分析&解答】39. Combination Sum

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  5. 【LeetCode题意分析&解答】38. Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  6. 【LeetCode题意分析&解答】43. Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  7. 【LeetCode题意分析&解答】42. Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. 【LeetCode题意分析&解答】36. Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  9. 【LeetCode题意分析&解答】34. Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

随机推荐

  1. Java RMI 学习笔记

    概况 功能:提供了客户辅助对象和服务辅助对象,为客户辅助对象创建和服务辅助对象形同的方法. 优点:客户不必写任何网络或I/O代码,调用远程方法就和运行在客户自己的本地JVM上对对象进行的正常方法一样. ...

  2. 有关va_list和vsnprintf输出函数的问题

    va_list ap; //声明一个变量来转换参数列表 va_start(ap,fmt); //初始化变量 va_end(ap); //结束变量列表,和va_start成对使用 可以根据va_arg( ...

  3. VMWARE使用问题

    因为一些原因创建的两个虚拟机出问题了,然而里面还放了好多东西呢不想就这样删掉,就抱着试一试的心态看能不能恢复(结果真能恢复). 这里使用的方法是VMware虚拟机配置文件(.vmx)损坏修复 在这过程 ...

  4. windbg命令学习2

    一.windbg查看内存命令: 当我们在调试器中分析问题时, 经常需要查看不同内存块的内容以分析产生的原因, 并且在随后验证所做出的假设是否正确. 由于各个对象的状态都是保存在内存中的, 因此内存的内 ...

  5. 如何彻底卸载sql server 2012

    好不容易装上了sql server 2012数据库,可是却不能连接本地的数据库,后来发现缺少一些服务,于是决定重新安装,但是卸载却很麻烦,如果卸载不干净的话,重新安装会出问题,所以下面就总结一些方法: ...

  6. 对web应用中单一入口模式的理解及php实现

    在我们web应用的开发中,经常会听见或看见单一入口模式,在我开始学习tp框架的时候也不理解为什么要运用一个单一入口模式,只是会使用,最近自己在搞一个小东西的时候才明白为什么在web开发中要运用单一入口 ...

  7. android开源框架和开源项目(转)

    特效: http://www.androidviews.net/ http://www.theultimateandroidlibrary.com/ 常用效果: 1. https://github.c ...

  8. #pragma anon_unions, #pragma no_anon_unions

    #pragma anon_unions, #pragma no_anon_unions 这些编译指示启用和禁用对匿名结构和联合的支持. 缺省设置 缺省值为 #pragma no_anon_unions ...

  9. 深入理解java虚拟机---读后笔记(垃圾回收)

    运行时数据区,主要包括方法区.虚拟机栈.本地方法栈.堆.程序计数器,该部分内存都是线程隔离的. 然后和其交互的有执行引擎.本地库接口,此部分线程之间是可以共享的. 1. 引用计数算法 给对象添加一个引 ...

  10. JAVA Layout

    /**  * baidu :组件不会直接放到框架上,而是放在若干个面板上,这些面板再放到窗格上?  * 实际上在JFrame上可直接添加Jbutton  *   * BorderLayout Flow ...