본문 바로가기
Algorithm/LeetCode

[LeetCode] 9. Palindrome Number

by yunamom 2022. 4. 17.
728x90
300x250

 

leetcode
9. Palindrome Number

📖문제. 숫자가 입력되면 해당 숫자가 팰린드롬이면 true, 아니면 false 를 출력하세요.

 

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.

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.
class Solution {
    public boolean isPalindrome(int x) {
        int num = x;
        int palindrome = 0;
        while(num > 0){          
            palindrome=palindrome*10+num%10;
            num/=10;
        }
        if(x == palindrome) return true;

    return false;   

    }   
}

 

728x90
300x250

코드