LeetCode - Algorithms - 9. Palindrome Number

一个多月前就看了下,这题label为Easy,跟 7. Reverse Integer 有关系,自己做7. Reverse Intege时就简单把整数转为字符串来解决,而这题提示Coud you solve it without converting the integer to a string,自己对c语言为例的过程化编程还是欠差,google 关键字 Palindrome Number,出来

最后参照 C Program to Check Whether a Number is Palindrome or Not 的写了java代码,在Eclipse里跑了下,结果不对,又看人家的代码,看来自己还是不理解这个求逆序数的步骤

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public boolean isPalindrome(int x) {
boolean b = false;
int originalInteger = x;
int reversedInteger = 0, remainder = 0;
int t = x;
while (t>0) {
remainder = t%10;
reversedInteger = reversedInteger*10+remainder;
t /= 10;
}
if (originalInteger==reversedInteger)
b = true;
return b;
}
}

Submission Detail

  • 11508 / 11508 test cases passed.
  • Your runtime beats 58.36 % of java submissions.