Skip to content

Instantly share code, notes, and snippets.

@u8989332
Created October 11, 2020 12:18
Show Gist options
  • Save u8989332/f87f5eb155bb383ed029469df7b48dfa to your computer and use it in GitHub Desktop.
Save u8989332/f87f5eb155bb383ed029469df7b48dfa to your computer and use it in GitHub Desktop.
LeetCode - 7 Reverse Integer
#include <iostream>
#include <cmath>
#include <climits>
using namespace std;
class Solution {
public:
int reverse(int x) {
long ans = 0;
while(x != 0){
ans = ans * 10 + (x % 10);
x /= 10;
}
return fabs(ans) > INT_MAX ? 0 : ans;
}
};
int main() {
Solution sol;
cout << sol.reverse(123) << endl;
cout << sol.reverse(-123) << endl;
cout << sol.reverse(1234567899) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment