Leetcode 9 - Palindrome number

Note:

  • Convert the int to a string might cause overflow.
  • Thought about using % but how do you know you’ve half way through it?
  • Well, when x < num, (num is the reversed second half), we’ve got the second half reversed.
  • Edge case: 10, 120, 110, etc. because we can’t have a leading zero.

Question:

Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward.

  • For example, 121 is a palindrome while 123 is not.

Example:

1
2
3
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.

Code:

1
2
3
4
5
6
7
8
9
10
11
public class Solution {
public bool IsPalindrome(int x) {
if (x < 0 || x % 10 == 0 && x != 0) return false;
int reversed = 0;
while (x > reversed) {
reversed = reversed * 10 + x % 10;
x /= 10;
}
return reversed == x || x == reversed/10;
}
}