leetcode007. Reverse Integer
1 /* a good way to predict overflow 2 * each time *10 must predict int overflow 3 * not only the last time to predict which will be wrong. 4 */ 5 class Solution { 6 public: 7 int reverse(int x) { 8 if(x==0)return x; 9 int tag=0; 10 long int re=0; 11 if(x<0) 12 { 13 tag=1; 14 x=-x; 15 } 16 while(x) 17 { 18 int tmp=re*10+x%10; 19 if((tmp-x%10)/10!=re)return 0; 20 re=tmp; 21 x=x/10; 22 } 23 if(tag)re=-re; 24 return re; 25 } 26 };

浙公网安备 33010602011771号