资讯专栏INFORMATION COLUMN

[LintCode] First Missing Positive

snifes / 1454人阅读

摘要:找第一个缺失的正整数,只要先按顺序排列好,也就是,找到第一个和不对应的数就可以了。注意数组的从开始,而正整数从开始,所以重写排列的时候要注意换成,而就是从开始的数组中的元素。

Problem

Given an unsorted integer array, find the first missing positive integer.

Example

Given [1,2,0] return 3,
and [3,4,-1,1] return 2.

Note

找第一个缺失的正整数,只要先按顺序排列好[1, 2, 3, 4, ...],也就是A[i] = i+1,找到第一个和A[i]不对应的数i+1就可以了。注意数组的index从0开始,而正整数从1开始,所以重写排列的时候要注意换成index-1,而index就是从A[0]开始的数组A[]中的元素。
我们看一个例子:

[2, 4, -1, 1] --> first for loop --> if (A[i] E (0, A.length) && A[i] != A[A[i]-1]) --> swap(A[i], A[A[i]-1]) --> [4, 2, -1, 1] --> [1, 2, -1, 4] --> second for loop --> A[2] != 3 --> return 2+1 = 3.

之前犯了一个错误,因为第二行A[i]的值已经变了,第三行再代入A[A[i]-1]就会出错:

int temp = A[i];
A[i] = A[A[i]-1];
A[A[i]-1] = temp;
Solution
public class Solution {
    public int firstMissingPositive(int[] A) {
        int len = A.length;
        for (int i = 0; i < len; i++) {
            if (A[i] > 0 && A[i] < len && A[i] != A[A[i]-1]) {
                int temp = A[A[i]-1];
                A[A[i]-1] = A[i];
                A[i] = temp;
                i--; //after the exchange, we need to speculate 
                     //and sort that digit again.
            }
        }
        for (int i = 0; i < len; i++) {
            if (A[i] != i + 1) return i+1; //as array index starts from 0 
                                           //and positive starts from 1.
        }
        return len + 1; //if the array has 1(A[0]) to len(A[len-1])
                        //and missed no one, return the len+1.
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65629.html

相关文章

  • leetcode 41. First Missing Positive

    摘要:题目要求在数组中找到第一个漏掉的正整数。思路一暴力排序后寻找排序后寻找显然是最快的。这些临时变量可以是排除出的量,也可以是有效量。当遇到的数字为有效数字时,则将该数字放到对应当前起始下标其相应的位置上。 题目要求 Given an unsorted integer array, find the first missing positive integer. For example,...

    smallStone 评论0 收藏0
  • [LeetCode] 41. First Missing Positive

    Problem Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2,0]Output: 3Example 2: Input: [3,4,-1,1]Output: 2Example 3: Input: [7,8,9,11,12]Output: 1Note...

    30e8336b8229 评论0 收藏0
  • LeetCode 之 JavaScript 解答第41题 —— 缺失的第一个正数(First Mis

    摘要:小鹿题目算法思路桶排序思想。再遍历数组,从下标开始判断该下标是否存放规定的数据,如果不是则该下标就是这组数据中缺失的最小正整数。桶排序还可以实现在一组数据中查找重复的数据。 Time:2019/4/6Title: First Missing PositiveDifficulty: DifficultyAuthor: 小鹿 题目:First Missing Positive Give...

    levius 评论0 收藏0
  • [LintCode] Interleaving Positive and Negative Numb

    摘要:注意,若正数多于负数,则序列以正数开始,正数结束。所以先统计正数个数,若超过序列长度的一半,则正指针从开始,反之则负指针从开始。注意交换函数的形式,必须是交换指针所指数字的值,而非坐标。 Problem Given an array with positive and negative integers. Re-range it to interleaving with positiv...

    calx 评论0 收藏0
  • [LintCode/LeetCode] Super Ugly Number

    摘要:建两个新数组,一个存数,一个存。数组中所有元素初值都是。实现的过程是,一个循环里包含两个子循环。两个子循环的作用分别是,遍历数组与相乘找到最小乘积存入再遍历一次数组与的乘积,结果与相同的,就将加,即跳过这个结果相同结果只存一次。 Problem Write a program to find the nth super ugly number. Super ugly numbers a...

    wuyumin 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<