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

# 02/01/2022 9

9\. Palindrome Number

Method 1:

convert the number into a char array. use two pointers to compare from left to right.&#x20;

Time O(n)

Space O(n)

class Solution {&#x20;

public boolean isPalindrome(int x) { char\[] check = String.valueOf(x).toCharArray(); int left = 0, right = check.length -1; while(left < right){ if(check\[left] != check\[right]) return false; left++; right--; } return true; } }

![](https://423021406-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mjjyb1BqScHCWaFFmF4%2Fuploads%2FfrDRIS1693vnGd0RR29N%2Fimage.png?alt=media\&token=5fb616ad-2bdc-43fe-a2b8-1b6399e9ef55)

Method 2:

reverse the last half number.  revertedNumber = revertedNumber \* 10 + x%10, x = x/10; check if reverted half is same with the first half digits of number.

Time O(log10n) n is the input value. or O(n) n is the length of the digits in this number.

Space O(1)

```
    class Solution { public boolean isPalindrome(int x) { 
    if(x < 0 || (x % 10 == 0 && x != 0 )) return false;
    int revert = 0;
    while(x > revert){
        revert = revert * 10 + x % 10;
        x /= 10;
    }
    return x == revert || x == revert/10;// when the length is odd
}
```

}

![](https://423021406-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mjjyb1BqScHCWaFFmF4%2Fuploads%2FCrCnIRZVjyaJbj1qZrfx%2Fimage.png?alt=media\&token=61e13745-7b19-4a6b-9c5b-014d70b306cc)
