本文共 980 字,大约阅读时间需要 3 分钟。
class Solution {//compare both the left most and the right most digit, //if not equal return falsepublic: bool isPalindrome(int x) { // Start typing your C/C++ solution below // DO NOT write int main() function if(x < 0) return false; int dvsNow = 1; while(x/dvsNow >= 10) dvsNow *= 10; while(x != 0) { int l = x/dvsNow; int r = x%10; if(l != r) return false; x = (x%dvsNow)/10;//take off both the left most and the right most digit dvsNow /= 100; } return true; }};
second time
class Solution {public: bool isPalindrome(int x) { // Start typing your C/C++ solution below // DO NOT write int main() function if(x < 0) return false; long long target = x; //if(target < 0) target = -target; long long reverse = 0; long long curNum = target; while(curNum != 0) { reverse = 10*reverse+curNum%10; curNum /= 10; } return reverse == target; }};
转载地址:http://fmxti.baihongyu.com/