본문 바로가기

Leetcode 100문제 도전

[Leetcode 4/100] Palindrome Number - Easy

https://leetcode.com/problems/palindrome-number/

 

Palindrome Number - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문자열로 푸는 방식

class Solution {
    public boolean isPalindrome(int x) {
        
        String str = String.valueOf(x);
        
        for(int i=0; i<str.length()/2; i++){
            if(str.charAt(i) != str.charAt(str.length()-i-1)) return false;
        }
        
        return true;
        
    }
}

솔루션

class Solution {
    public boolean isPalindrome(int x) {
        
        if(x < 0) return false;
        
        int y = x;
        int res = 0;
        
        while(y != 0){
            res = res * 10 + y % 10;
            y /= 10;
        }
        
        return x == res;
        
    }
}