About the "First Missing Positive"


  • 0
    H

    Hi, everyone,

    My code is that:


    public class Solution {
        public int firstMissingPositive(int[] A) {
            int len=A.length;
            int tmp,i=0,j=0;
            while (i<len) {
                tmp=A[i];
                if (tmp<=0 || tmp>len) {
                    A[i]=0;i++;
                }
    	        else if (tmp==i+1)
    		        i++;
                else {
                    j=tmp-1;
                    tmp=A[j];
                    A[j]=j+1;
                    if (tmp>0 && tmp<=len)
                        A[i]=tmp;
                    else
                        A[i]=0;
                }
            }
            for (i=0;i<len;i++) {
                if (A[i]!=i+1)
                    return (i+1);
            }
            return len+1;
        }
    }
    

    Abviously, if the input is "[]", "len" will be 0, it will return 1 directly. I also make it on the "JDK".

    However, when I submit it , it always shows:

    Submission Result: Time Limit Exceeded
    Last executed input: []

    Who can tell me why?
    Thanks in advance!

    hmyu


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.