TIL3.LeetCode_Palindrome Number

chloe·2021년 7월 13일
0

TIL

목록 보기
71/81
post-thumbnail

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 palindrome while 123 is not.

Example 1:
Input:X=121
Output:true

Example2:
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.

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

Example 4:
Input : x=-101
Output:false

var isPalindrome = function(x) {
    if(x<0){
        return false;
    }
    let number =x;
    let reverse=0;
    while(number>0){
        reverse=reverse*10+number%10;
        number=parseInt(number/10)
    }
    return x ===reverse;
};

또 다른 풀이 방법

var isPalindrome = function(x) {
  let reversed = x.toString().split('').reverse().join('');
    return (x.toString() === reversed)
};```
profile
Front-end Developer 👩🏻‍💻

0개의 댓글