丟失的第一個正整數_LintCode

public class Solution {
    /**    
     * @param A: an array of integers
     * @return: an integer
     */
    public int firstMissingPositive(int[] A) {
      // write your code here
        if (A==null||A.length<=0){
            return 1;
        }

        if (A.length==1){
            if (A[0]==1){
                return 2;
            }
            else  {
                return 1;
            }
        }
            Arrays.sort(A);
        if (A[0]>1){
            return 1;
        }
            int length = A.length-1;
            for (int i=0;i<length;i++){
                 if (A[i]>=0){
                     if (A[i+1]-A[i]>1){
                     return A[i]+1;
                 }
                }
            }
            return A[length]+1;



    }
}
發佈了43 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章