Reverse Integer
Input: x = 123
Output: 321Input: x = -123
Output: -321Input: x = 120
Output: 21Input: x = 0
Output: 0Solutions
π§ Cpp
Last updated
Input: x = 123
Output: 321Input: x = -123
Output: -321Input: x = 120
Output: 21Input: x = 0
Output: 0Last updated
class Solution {
public:
int reverse(int x)
{
bool is_neg = x < 0;
std::string num = std::to_string(x);
std::reverse(num.begin()+is_neg, num.end());
long res = std::atol(num.c_str());
return res > numeric_limits<int>::max() || res < numeric_limits<int>::min() ?
0 : res;
}
};