Home LeetCode. 9. Palindrome Number
Post
Cancel

LeetCode. 9. Palindrome Number

[Link] https://leetcode.com/problems/palindrome-number/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
    public boolean isPalindrome(int x) {
        if(x < 0) return false;
        String s = String.valueOf(x);
        int l = s.length();
        boolean isPalindrome = true;
        if(l % 2 == 0) { //0 1 2 3
            for(int i = 0; i <= l/2 - 1; i++) {
                if(s.charAt(i) != s.charAt(l - i - 1)) return false;
            }
        } else {
            for(int i = 0; i <= l/2 - 1; i++) { // 0 1 2 (3)
                if(s.charAt(i) != s.charAt(l - i - 1)) return false;
            }
        }
        return true;
    }
}
This post is licensed under CC BY 4.0 by the author.