> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/math/9.-palindrome-number.md).

# 9. Palindrome Number

![](/files/-MeXbuIMHJW4_4QbDnyz)

![](/files/-MeXbzAR5IvGVAea3afL)

## brute force: convert the integer to a string

time: O(n/2)

space: O(1)

```java
class Solution {
    public boolean isPalindrome(int x) {
        // first instinct, parse into String, reverse, check is it the same
        String s = String.valueOf(x);
        char c[] = s.toCharArray();
        for (int i = 0; i < c.length/2; i++) {
            char temp = c[i];
            c[i] = c[c.length - i - 1];
            c[c.length - i - 1] = temp;
        }
        return String.valueOf(c).equals(s);
    }
}
```

## Math

time: O(logn), log base is 10

space: O(1)

```java
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 res == x;
    }
}
```
