判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

 


 x = 12321;  divH= 10000;更新divH=divH/100;

High = 12321/10000=1

Low = 12321%10=1

更新x = (12321%10000-Low)/10=232

High = 232/100=2;

Low = 232%10=2;

更新x = (232%100-Low)/10 = 3

High = 3/1;

Low = 3%10;

更新x = (3%1-Low)/10=0;

减Low可省略

 1 class Solution {
 2 public:
 3     bool isPalindrome(int x) {
 4         if(x<0) return false;//处理负数情况
 5         int divH=1;
 6         while((x/divH)>9){
 7             divH = divH*10;
 8         }
 9         
10         while(x>0){
11             int High = x/divH;
12             int Low = x%10;
13             if(High!=Low) return false;
14             x = (x%divH-Low)/10;
15             divH =divH/100;               
16         }                 
17         return true;
18         
19     }
20 };

 

posted on 2019-01-19 15:34  柠檬檬檬  阅读(111)  评论(0编辑  收藏  举报