标签:style blog color io div sp log on c
Determine whether an integer is a palindrome. Do this without extra space.
思路:先找出x的有效最高位,然后从两侧依次比较最高位和最低位即可。
1 class Solution { 2 public: 3 bool isPalindrome( int x ) { 4 if( x < 0 ) { return false; } 5 int high = 1; 6 while( x/high >= 10 ) { high *= 10; } 7 while( high >= 10 ) { 8 if( x/high != x%10 ) { return false; } 9 x = ( x % high ) / 10; 10 high /= 100; 11 } 12 return true; 13 } 14 };
标签:style blog color io div sp log on c
原文地址:http://www.cnblogs.com/moderate-fish/p/3960679.html