# 9. Palindrome Number

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-MeXamQFQoHZUdZwx90q%2F-MeXbuIMHJW4_4QbDnyz%2Fimage.png?alt=media\&token=086b16b7-c998-4a7b-8b0d-5a0b12b83fbd)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-MeXamQFQoHZUdZwx90q%2F-MeXbzAR5IvGVAea3afL%2Fimage.png?alt=media\&token=ac13d11d-f3ef-4185-99ce-6c997c6bdf48)

## 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;
    }
}
```
