본문 바로가기
Problem Solving/LeetCode

[LeetCode] 9. Palindrome Number - Java

by graycode 2026. 5. 7.

 문제 링크

 

Palindrome Number - LeetCode

Can you solve this real interview question? Palindrome Number - Given an integer x, return true if x is a palindrome, and false otherwise.   Example 1: Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left. Ex

leetcode.com

 

 풀이 코드

public class Solution {

    public boolean isPalindrome(int x) {
        if (x < 0) return false;

        int sum = 0, n = x;
        do sum = sum * 10 + n % 10; while ((n /= 10) > 0);

        return x == sum;
    }

}

댓글