257. PermCheck

아현·2021년 8월 14일
0

Algorithm

목록 보기
269/400



1. JavaScript

정해 (예외처리)



function solution(A) {
  
    A.sort((l, r) => l - r);
  
    if (A[0] !== 1) {
      return 0;
    }
  
    for (let i = 0; i < A.length - 1; i++) {
        if (A[i + 1] - A[i] !== 1) {
            return 0;
        }
    }
    return 1;
}




50%




// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');

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

    return 1;
}




2. Python


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

def solution(A):
    A.sort()
    
    if A[0] != 1:
        return 0
    
    for i in range(len(A) - 1):
        if (A[i + 1] - A[i]) != 1:
            return 0
    
    return 1
        



profile
For the sake of someone who studies computer science

0개의 댓글