1. Given an unsorted integer array, find the smallest missing positive integer.
  2.  
  3. Example 1:
  4.  
  5. Input: [1,2,0]
  6. Output: 3
  7. Example 2:
  8.  
  9. Input: [3,4,-1,1]
  10. Output: 2
  11. Example 3:
  12.  
  13. Input: [7,8,9,11,12]
  14. Output: 1
  15. Note:
  16.  
  17. Your algorithm should run in O(n) time and uses constant extra space.

虽然不能再另外开辟非常数级的额外空间,但是可以在输入数组上就地进行swap操作。

思路:交换数组元素,使得数组中第i位存放数值(i+1)。最后遍历数组,寻找第一个不符合此要求的元素,返回其下标。整个过程需要遍历两次数组,复杂度为O(n)

下图以题目中给出的第二个例子为例,讲解操作过程。

  1. class Solution {
  2. public int firstMissingPositive(int[] nums) {
  3. if(nums == null || nums.length == 0){
  4. return 1;
  5. }
  6.  
  7. for (int i=0; i<nums.length; i++){
  8. if(nums[i] <= nums.length && nums[i] > 0 && nums[i] != nums[nums[i]-1]){
  9. int temp = nums[nums[i]-1];
  10. nums[nums[i]-1] = nums[i];
  11. nums[i] = temp;
  12. i--;
  13. }
  14. }
  15. for(int i=0; i<nums.length; i++){
  16. if(nums[i] != i+1){
  17. return i+1;
  18. }
  19. }
  20. return nums.length+1;
  21. }
  22. }

LeetCode – First Missing Positive的更多相关文章

  1. [LeetCode] First Missing Positive 首个缺失的正数

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

  2. Leetcode First Missing Positive

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

  3. LeetCode: First Missing Positive 解题报告

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

  4. LeetCode OJ-- First Missing Positive

    https://oj.leetcode.com/problems/first-missing-positive/ 给一列数,找出缺失的第一个正数.要求时间复杂度 O(n) 第一步遍历一遍,找出最大的数 ...

  5. leetcode First Missing Positive hashset简单应用

    public class Solution { public int firstMissingPositive(int[] A) { HashSet<Integer> hash=new H ...

  6. leetcode First Missing Positive python

    class Solution(object): def firstMissingPositive(self, nums): """ :type nums: List[in ...

  7. leetcode:First Missing Positive分析和实现

    题目大意: 传入整数数组nums,求nums中未出现的正整数中的最小值.要求算法在O(n)时间复杂度内实现,并且只能分配常量空间. 分析: 一般碰到这种问题,都先对数组进行排序,再遍历数组就可以找到最 ...

  8. [LeetCode]题解(python):041-First Missing Positive

    题目来源 https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the ...

  9. leetcode 41 First Missing Positive ---java

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

随机推荐

  1. 首席科学家马丁•福勒(Martin Fowler)

    现任思特沃克公司首席科学家的马丁·福勒先生是当今世界软件开发领域最具影响力的五位大师之一.作为一位敏捷软件开发方法的早期开拓者,福勒先生对IT 业的影响是不可估量的. 思特沃克公司是一家跨国专业IT ...

  2. awk计算最大值,最小值,平均值的脚本

    传入至少三个数字参数到脚本awk_file,并计算出最大,最小,平均值.需要判断传入的数字是否足够,否则输出警告信息.平均值保留两位小数. 如执行bash awk_file 3 4 6 5,脚本输出结 ...

  3. day18 类与类之间的关系

    今日所学内容: 1.类与类之间的关系 2.self 到底是谁? 3. 特殊成员 : 1.类与类之间的关系 在我们的世界中事物和事物之前总会有一些联系 在面向对象中,类与类之间也可以产生相关的联系 1) ...

  4. 下载python中package的简便方法

    pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xxx

  5. MFC 中GetClientRect、ClientToScreen、GetWindow、RectScreenToClient的使用

    CWnd* pWnd = GetDlgItem(IDB_BUT_RECOGNIZE); pWnd->GetClientRect(&rect);   //指该控件自身客户区的矩形,原点为控 ...

  6. 关于macroblaze的一些理解(更新中)

    (1)添加*.elf文件: 在Design Sources工作目录中右键选择添加源文件,找到SDK目录中对应的文件夹下的Debug内*.elf文件,将其添加.然后,源文件目录更新,多出一个ELF文件夹 ...

  7. DevExpress ASP.NET Bootstrap Controls v18.2新功能详解(一)

    行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExpress ASP.NET Boot ...

  8. 解决无法创建 JPA 工程的问题

    原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7703803.html ------------------------------------ ...

  9. JavaWeb基础-认识JavaWeb

    程序开发体系 B/S 浏览器/服务器 开发维护成本低 客户端负载低 安全性低 C/S 客户端/服务器 成本高 客户端负载高 安全性高 javaweb简介 静态网页 HTML CSS,人浏览的数据是始终 ...

  10. CSS学习笔记-01-2D转换模块

    首先,mark 一下  css3 新增 的 2D 转换之 W3school 的链接: http://www.w3school.com.cn/css3/css3_2dtransform.asp 转换是使 ...