Created
October 11, 2020 12:18
-
-
Save u8989332/f87f5eb155bb383ed029469df7b48dfa to your computer and use it in GitHub Desktop.
LeetCode - 7 Reverse Integer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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