First Missing Positive leetcode java

题目

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.

题解

题目给了一个unsorted integer array。当然,可以用先排好序的方法走(但是时间复杂度就不能是O(n))。

所以这里提供一个不先排序的算法。

注意:题目要求是find the first missing positive integer

也就是说,即便你给的数组是4 5 6 7,看似都一一排好序,但是返回值一定是1,也就是如果给的数组是4 5 7 8 ,答案不是6,是1。

因此,有了这个性质,我们就能i和A[i]是否相等来做判断了。“实现中还需要注意一个细节,就是如果当前的数字所对应的下标已经是对应数字了,那么我们也需要跳过,因为那个位置的数字已经满足要求了,否则会出现一直来回交换的死循环。”引自 Code Ganker

代码如下:

1     private void swap(int[] A, int i, int j){
 2         if(A[i]!=A[j]){
 3             A[i]^=A[j];
 4             A[j]^=A[i];
 5             A[i]^=A[j];
 6         }
 7     }
 8     public int firstMissingPositive(int[] A) {
 9         if(A.length==0||A==null)
10             return 1;
11             
12         for (int i = 0; i < A.length; i++){
13             if (i != A[i]){
14                 if (A[i] <= 0 || A[i] > A.length-1 || A[i] == A[A[i]])
15                     continue;
16                 else{
17                     swap(A, i, A[i]);
18                     i--;
19                 }
20             }
21         }
22         int k = 1;  
23         while (k < A.length && A[k] == k) 
24             k++;  
25             
26         if(A[0]==k)
27             return k+1;
28         else
29             return k;
30     }

First Missing Positive leetcode java

时间: 2024-10-13 16:53:51

First Missing Positive leetcode java的相关文章

41. First Missing Positive Leetcode Python

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. 这题用的是count sort 的方法. 首先我们来复习一下count sort 已知一个正整数还未

First Missing Positive -- leetcode

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. class Solution { public: int firstMissingPositive(

Java for LeetCode 041 First Missing Positive

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. 解题思路一: 刚看到题目的时候感觉无从下手,后来仔细理解题意,需要找到first missing p

leetcode:41. First Missing Positive (Java)

转载请注明出处:z_zhaojun的博客 原文地址 题目地址 First Missing Positive 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 spa

41. First Missing Positive【leetcode】寻找第一个丢失的整数,java,算法

41. First Missing Positive 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,2,4,5,

[LeetCode][Java] First Missing Positive

题目: 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,2,0] 返回

LeetCode OJ:First Missing Positive (第一个丢失的正数)

在leetCode上做的第一个难度是hard的题,题目如下: 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. 关键是要实现0(N)的时间复杂度以及常数级

[Leetcode][Python]41: First Missing Positive

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 41: First Missing Positivehttps://oj.leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,a

LeetCode: First Missing Positive 解题报告

First Missing Positive 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. SOLUTION 1: 使用类似桶排序的方法: 将值放在它