public bool IsPalindrome(int x) { string str = x.ToString(); for (int i = 0; i < str.Length / 2; i++) { if (str[i] != str[str.Length - i - 1]) { return false; } } return true; }
反转整数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
public bool IsPalindrome(int x) { if (x < 0) { return false; } else { int num = x; int result = 0; while (num != 0) { result = result * 10 + num % 10; num /= 10; } return result == x ? true : false; } }