9. Palindrome Number Kai 2025-06-15 LeetCode Math, String My Solution (String Builder and compare)1234567891011121314class Solution { public boolean isPalindrome(int x) { // convert to string String str = String.valueOf(x); // new string builder StringBuilder sb = new StringBuilder(); // save from tight to left for (int pos = str.length() - 1; pos >= 0; pos--) { sb.append(str.charAt(pos)); } // compare strings return str.equals(sb.toString()); }} Math123456789101112131415class Solution { public boolean isPalindrome(int x) { // negative & end with `0` if (x < 0 || (x % 10 == 0 && x != 0)) return false; int reverted = 0; while (x > reverted) { reverted = reverted * 10 + x % 10; x /= 10; } // caompare first half and last half return x == reverted || x == reverted / 10; }} Remarks: Use reverted/10 to ignore the middle number (if odd)