Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions C++/chapImplement.tex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down