[leet-code] Palindrome Number

Rae-eun Yang·2022년 7월 13일
0

leet-code

목록 보기
2/2

Description


Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward.

  • For example, 121 is a palindrome while 123 is not.

Examples


Example 1:

Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.

Example 2:

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Constraints:

  • -231 <= x <= 231 - 1

해석


정수 x가 주어지면 x가 펠린드롬 수이면 true를 반환합니다.

펠린드롬 수는 정방향과 역방향이 같은 수 입니다.

예를 들어, 121은 펠린드롬 수이지만 123은 그렇지 않습니다.


풀이 코드 (JavaScript)

/**
 * @param {number} x
 * @return {boolean}
 */
var isPalindrome = function(x) {
    
    num = x.toString();
    for(var i = 0; i < num.length; i++) {
        if(num[i] != num[num.length - 1 - i])
            return false;
    }
    
    return true;
    
};

한번에 성공!
.toString() : 문자열로 변환
-> 이런건 자바랑 비슷하구나~~ 생각했다 (자바보다 간단한듯)


profile
개발자 지망생의 벨로그

0개의 댓글