diff --git a/C++/chapImplement.tex b/C++/chapImplement.tex index 6c0aeab8..fcd8ae7b 100644 --- a/C++/chapImplement.tex +++ b/C++/chapImplement.tex @@ -33,15 +33,26 @@ \subsubsection{代码} \begin{Code} //LeetCode, Reverse Integer // 时间复杂度O(logn),空间复杂度O(1) +// 考虑 1.负数的情况 2. 溢出的情况(正溢出&&负溢出,比如 x = -2147483648(即-2^31) ) class Solution { public: int reverse (int x) { - int r = 0; - - for (; x; x /= 10) - r = r * 10 + x % 10; - - return r; + long long r = 0; + long long t = x; + t = t > 0 ? t : -t; + for (; t; t /= 10) + r = r * 10 + t % 10; + + bool sign = x > 0 ? false: true; + if (r > 2147483647 || (sign && r > 2147483648)) { + return 0; + } else { + if (sign) { + return -r; + } else { + return r; + } + } } }; \end{Code}