Title Source: Leetcode Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:example 2:example 3:
input : 123 input : -123 Input: 120
output: 321 output : -321 output: 21
Note:
Assume we is dealing with a environment which could only store integers within the 32-bit signed integer range: [? 231, 231? 1]. For the purpose of this problem, assume a your function returns 0 when the reversed integer overflows.
Ideas:
- Consider three different scenarios:
-
- The value itself overflows.
- The value is going to overflow, the demarcation is the same as the int_max digit in unknown number.
- The value is within a safe range.
-
- The overflow is determined first, if the overflow returns directly 0
- Edge-by-bit comparison, side reversal, judgment overflow, if overflow returns 0
- Direct reverse
Prerequisite Processing:
1 int m=x,i=1, ten=1,//i record digit, ten record maximum number of 2while (m/=10 ) {++i;ten*=;} 3 if (i<) m=reversemin (x,i,ten); 4 Else M=reversemax (x,i,ten);
To determine the value overflow:
1 unsigned int y = x;//Extended Range
2 if (x<0) y=-x;
3 if (Y>int_max) return 0;
Bitwise comparison, and reverse:
1 intReversemax (intXintIintTen)2 {3 intsign=1;4 if(x<0) {x=-x; sign=-1;} Negative number becomes positive, sign restores negative number5 intm=0, max=Int_max;6 while(i--) {7 intRest = x%Ten;8 intmrest=max/Ten;9 if(rest>mrest)return 0;//compared to Int_max bitwise, if greater than is already overflow, return 0Ten Else if(rest<mrest) Break;//If it is less than the safe range, jump to a safe reverse OneM + = rest *ten;//The next comparison is required only in equal circumstances AX/=Ten; -max=max-mrest*Ten; -Ten/=Ten; the } - if(ten==0)returnm*Sign ; -++i;//last loop abort, no action, so restore the number of deleted bits - + while(i--) {//Normal reverse - intRest = x%Ten; +M + = rest *Ten; AX/=Ten; atTen/=Ten; - } - returnm*Sign ; - -}
! Negative number after modulo is negative, can not be compared, so first take positive.
! Determine whether to reverse the end, the 16th line, can not use the number of digits, because the break out of the number of bits also minus 1
Direct reverse:
1 intReversemin (intXintIintTen)2 { 3 intm =0; 4 while(i--) { 5 intRest = x%Ten; 6M + = rest *Ten;7X/=Ten; 8Ten/=Ten; 9 }Ten returnm; One}
Overall code:
1 classSolution {2 Public:3 intReverseintx) {4Unsignedinty =x;5 if(x<0) y=-x;6 if(Y>int_max)return 0;7 intReversemin (intXintIintten);8 intReversemax (intXintIintten);9 intM=x,i=1, ten=1;Ten while(m/=Ten) {++i;ten*=Ten;} One if(i<Ten) m=reversemin (x,i,ten); A Elsem=Reversemax (x,i,ten); - returnm; - } the }; - intReversemin (intXintIintTen) - { - intm =0; + while(i--) { - intRest = x%Ten; +M + = rest *Ten; AX/=Ten; atTen/=Ten; - } - returnm; - } - intReversemax (intXintIintTen) - { in intsign=1; - if(x<0) {x=-x; sign=-1;} to intm=0, max=Int_max; + while(i--) { - intRest = x%Ten; the intmrest=max/Ten; * if(rest>mrest)return 0; $ Else if(rest<mrest) Break;Panax NotoginsengM + = rest *Ten; -X/=Ten; themax=max-mrest*Ten; +Ten/=Ten; A } the if(ten==0)returnm*Sign ; +++i; - $ while(i--) { $ intRest = x%Ten; -M + = rest *Ten; -X/=Ten; theTen/=Ten; - }Wuyi returnm*Sign ; the -}
[C + +] range control issues with inverted 32-bit int integers