1. Description of the problem
Reverse digits of an integer.
EXAMPLE1:X = 123, return 321
example2:x = -123, return-321
Click to show spoilers.
Has a thought about this?
Here is some good questions to ask before coding. Bonus points for if you have already thought through this!
If the last digit are 0, what should the output being? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer and then the reverse of 1000000003 overflows. How should handle such cases?
For the purpose of this problem, assume a your function returns 0 when the reversed integer overflows.
Solution.
class Solution {public: int reverse (int x) { }};
2. Answer your ideas
2.1. Why is there overflow?
How integers are stored: the highest bit is the sign bit
- 16-bit integer range: -2^15~2^15-1, 32768 to 32767
- 32-bit integer range: -2^31~2^31-1, 2147483648 to 2147483647
2.2. How to determine overflow?
- The range of incoming integer x is: 2147483648 to 2147483648, after reversal (no overflow problem is considered): -2147483648-- -8463847412; 2147483647-->7463847412.
- Known by the range of 32-bit integers, it is possible to overflow if and only if the integer x is a 10-digit number.
- When x is 10 digits, consider the bit, if x%10 > 2, then overflow, if x%10 < 2, then not overflow, if x%10 = = 2, you need to consider the X 10 bits. If 10 bits is 1, you need to consider the hundred, and so on.
3. Code
1 classSolution {2 Public:3 intReverseintx) { 4 BOOLbispositive = x >0;5x = bispositive? ×:-X;//can also call the absolute value of the function abs (x)6 7 if(Isoverflow (x))//Handling Overflow8 {9 return 0;Ten } One A intresult =0; - while(0!=x) - { theresult = result *Ten+ x percentTen; -x = x/Ten; - } - if(!bispositive) + { -result =-result; + } A at returnresult; - } - Private: - /*! \FN bool Isoverflow (int x) - * \brief Determines if the input integer x is overflow. - * \param[in] x 32-bit integer entered by the user. in * \return results. - *-\b true overflow. to *-\b false not overflow. + */ - BOOLIsoverflow (intx) the { * if(-2147483648= = x)//Note that the absolute value of 2147483648 is overflow, and-X is still not-2147483648, so it needs to be judged separately. $ {Panax Notoginseng return true; - } the + intNbitcount =0;//record the number of digits of the current input integer A inttx =x; the while(0!=tx) + { -nbitcount++; $tx = tx/Ten; $ } - if(Ten= = Nbitcount)//2147483647 1463847412 - { the if(Recisoverflow (x)) - {Wuyi return true; the } - } Wu - return false; About } $ /*! \FN bool Recisoverflow (int x, int idx = 1463847412) - * \brief recursion to determine if the input integer x is overflow. - * \param[in] x 32-bit integer entered by the user. - * \param[in] idx is used to determine the integer that overflows. A * \return results. + *-\b true overflow. the *-\b false not overflow. - */ $ BOOLRecisoverflow (intXintIDX =1463847412) the { the intX_remainder = x%Ten; the intIdx_remainder = idx%Ten; the - if(X_remainder >Idx_remainder) in { the return true; the } About Else if(X_remainder = = Idx_remainder && x!=0&& idx!=0) the { the returnRecisoverflow (x/Ten, idx/Ten); the } + return false; - } the};
4. Reflection
For-2147483648, because of its absolute value overflow,-X is still not-2147483648, it needs to be judged separately.
7. Reverse Integer