1 static int wing=[](){ 2 std::ios::sync_with_stdio(false); 3 cin.tie(NULL); 4 return 0; 5 }(); 6 7 class Solution 8 { 9 public: 10 bool isPalindrome(int x) 11 { 12 if(x<0) 13 return false; 14 int copy=x,reverse=0; 15 while(copy) 16 { 17 reverse=reverse*10+copy%10; 18 copy/=10; 19 } 20 return reverse==x; 21 } 22 };
1 static int wing=[](){ 2 std::ios::sync_with_stdio(false); 3 cin.tie(NULL); 4 return 0; 5 }(); 6 7 class Solution 8 { 9 public: 10 bool isPalindrome(int x) 11 { 12 if(x==0) 13 return true; 14 if(x<0||(x%10)==0) 15 return false; 16 int copy=x,reverse=0; 17 while(copy>reverse) 18 { 19 reverse=reverse*10+copy%10; 20 copy/=10; 21 } 22 return copy==reverse||copy==reverse/10; 23 } 24 };
两种方法,比较好懂,
第一种是把原数字全部逆置,再和原数比较
第二种是逆置后半段,与前半段比较,但是这种方法要排除末尾是0的情况
 
                    
                     
                    
                 
                    
                 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号