题目描述:

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.

解题思路:

参考http://blog.csdn.net/nanjunxiao/article/details/12973173

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

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

代码如下:

public class Solution {
public int firstMissingPositive(int[] nums) {
int length;
int temp; if (nums == null || (length = nums.length) == 0)
return 1;
for (int i = 0; i < length; i++) {
if (nums[i] == i + 1)
continue;
else {
while (nums[i] > 0 && nums[i] <= length
&& nums[i] != nums[nums[i] - 1]) {
temp = nums[i];
nums[i] = nums[nums[i] - 1];
nums[temp - 1] = temp;
}
}
}
for (int i = 0; i < length; i++) {
if (nums[i] != i + 1)
return i + 1;
} return length + 1;
}
}

  

Java [Leetcode 41]First Missing Positive的更多相关文章

  1. [array] leetcode - 41. First Missing Positive - Hard

    leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...

  2. LeetCode - 41. First Missing Positive

    41. First Missing Positive Problem's Link ---------------------------------------------------------- ...

  3. leetcode 41 First Missing Positive ---java

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

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

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  5. [leetcode]41. First Missing Positive第一个未出现的正数

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  6. [LeetCode] 41. First Missing Positive ☆☆☆☆☆(第一个丢失的正数)

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  7. leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法

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

  8. LeetCode 41 First Missing Positive(找到数组中第一个丢失的正数)

    题目链接: https://leetcode.com/problems/first-missing-positive/?tab=Description   给出一个未排序的数组,求出第一个丢失的正数. ...

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

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

随机推荐

  1. 1043. Is It a Binary Search Tree

    http://www.patest.cn/contests/pat-a-practise/1043 #include <stdio.h> #include <vector> u ...

  2. 2014年辛星完全解读Javascript第八节 json

    json是JavaScript Object Notation的简写,它是一种轻量级的数据交换格式,而且表达上很容易靠字面去理解.json是用于存储和传输数据的格式,通常用于向服务器端传递数据. ** ...

  3. 十八、mysql 内存优化 之 myisam

    .key_buffer 索引块大小 set global hot_cache.key_buffer_size = ; //设置大小 show variables like 'key_buffer_si ...

  4. Linux配置Tomcat(转载)

    转载自:http://www.cnblogs.com/zhoulf/archive/2013/02/04/2891633.html 安装说明 安装环境:CentOS-6.3安装方式:源码安装 软件:a ...

  5. Unity3d 如何找到游戏对象并改变其颜色

    //游戏对象 private var obj:GameObject; //渲染器 private var render:Renderer; //贴图 private var texture:Textu ...

  6. 第一个js库文件

    <!DOCTYPE html> <html xmlns=;         ;                     }                 };     })(); ...

  7. bnu 4358 左手定则 (搜索)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=4358 [题意]:给定起始位置和方向和目的地,按照左转.前进.右转.后退的优先级递减,也就是说能左转就 ...

  8. org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.chen.vo.Dept.parentId

    异常描述:执行以下的addAsHaveParentId2方法出现此异常: /*-----------------------类Dept.Dept.hbm.xml有parentId属性(数据库中有此列) ...

  9. window.location.hash属性介绍

    location是javascript里边管理地址栏的内置对象,比如location.href就管理页面的url,用location.href=url就可以直接将页面重定向url.而location. ...

  10. SDUT 2523 OOXX

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2523 思路 :就是先统计一下方阵中1多少2多少 ...