256. MissingInteger

아현·2021년 8월 14일
0

Algorithm

목록 보기
268/400



1. JavaScript

정해


function solution(A) {
    A.sort((a, b) => a - b);
    
    let min = 1;
    
    for (let i in A) {
        if (A[i] > 0 && A[i] == min) {
            min++;
        }
    }
    
    return min;
}


33%



function solution(A) {
    // write your code in JavaScript (Node.js 8.9.4)
    let list = new Set(A);
    list = [...list];
    list.sort((a, b) => a - b);
    if (list.slice(-1) < 0) {
        return 1
    } else {
        for (i = 0; i < list.length; i++) {
            if (list[i] + 1 != list[i + 1]) {
                return list[i] + 1
            }
        }
    }

}



2. Python


# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    A.sort()
    min = 1
    for i in range(len(A)):
        if A[i] > 0 and A[i] == min:
            min += 1
    
    return min
profile
For the sake of someone who studies computer science

0개의 댓글