博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
First Missing Positive Given an unsorted integer array, find the first missing positive integer. Fo
阅读量:4075 次
发布时间:2019-05-25

本文共 1010 字,大约阅读时间需要 3 分钟。

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.

Java代码:

public class Solution {   public int firstMissingPositive(int[] A) {    if (A == null || A.length == 0) {        return 1;    }    // Put the corresponding positive value in the index that equals to its value    int len = A.length;    for (int i = 0; i < len;) {        int num = A[i];        if (num > 0 && num < len && num != i && num != A[num]) {            A[i] = A[num];            A[num] = num;        } else {            ++i;        }    }    // Scan the array to find the first value that is not equal to its index, then it is the missing value    // Test small examples first    int missingValue = A[0] == len ? len+1 : len;    for (int i = 1; i < len; ++i) {        if (A[i] != i) {            missingValue = i;            break;        }    }    return missingValue;}}

转载地址:http://csuni.baihongyu.com/

你可能感兴趣的文章
场景中,并没有灯源的存在,但是cube却会有灯光照射的反应,这就是Light Probe Group的作用。...
查看>>
unity3d 为什么要烘焙?烘焙作用是为了什么?
查看>>
[AR]高通Vuforia Getting Started
查看>>
Unity ios、android、pc一键打包(一)
查看>>
欧几里得空间
查看>>
今天写shader流光效果,shader代码少了个括号,unity shader compiler卡死且不提示原因...
查看>>
Unity shader saturate
查看>>
_LightColor0将会是主要的directional light的颜色。
查看>>
Unity的Shader如何控制投影颜色
查看>>
Unity3d 下websocket的使用
查看>>
unity与android交互总结
查看>>
android studio 开发android app 真机调试
查看>>
两分钟彻底让你明白Android Activity生命周期(图文)!
查看>>
这几天用高通VUFORIA的体会
查看>>
ARGB_8888
查看>>
HoloLens的显示分辨率有多少?
查看>>
ThreadPool(线程池) in .Net
查看>>
为什么3D模型的网格由很多三角形来组成
查看>>
U3D OnDrawGizmos
查看>>
Unity Remote 5 使用
查看>>