21 lines
366 B
C++
21 lines
366 B
C++
#include "s0007_reverse_integer.hpp"
|
|
|
|
int Solution::reverse(int x) {
|
|
int r{0};
|
|
std::queue<int> queue;
|
|
|
|
while (x != 0) {
|
|
queue.push(x % 10);
|
|
x = static_cast<int>((x - (x % 10)) / 10);
|
|
}
|
|
while (!queue.empty()) {
|
|
if (r < INT_MIN / 10 || r > INT_MAX / 10) {
|
|
return 0;
|
|
}
|
|
r = r * 10 + queue.front();
|
|
queue.pop();
|
|
}
|
|
|
|
return r;
|
|
}
|