Reverse Integer Solutions in C++
Number 7
Difficulty Easy
Acceptance 25.8%
Link LeetCode
Other languages Go
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/reverse-integer/// Author : Hao Chen// Date : 2014-06-18#include <stdio.h>#include <stdlib.h>//Why need the INT_MIN be defined like that?//Please take a look:// http://stackoverflow.com/questions/14695118/2147483648-0-returns-true-in-c#define INT_MAX 2147483647#define INT_MIN (-INT_MAX - 1)int reverse(int x) {int y=0;int n;while( x != 0){n = x%10;//Checking the over/underflow.//Actually, it should be y>(INT_MAX-n)/10, but n/10 is 0, so omit it.if (y > INT_MAX/10 || y < INT_MIN/10){return 0;}y = y*10 + n;x /= 10;}return y;}#define TEST(n, e) printf("%12d => %-12d %s!\n", n, reverse(n), e == reverse(n)?"passed":"failed")int main(int argc, char**argv){//basic casesTEST( 123, 321);TEST( -123, -321);TEST( -100, -1);TEST( 1002, 2001);//big integerTEST( 1463847412, 2147483641);TEST(-2147447412, -2147447412);TEST( 2147447412, 2147447412);//overflowTEST( 1000000003, 0);TEST( 2147483647, 0);TEST(-2147483648, 0);//customized casesif (argc<2){return 0;}printf("\n");for (int i=1; i<argc; i++) {int n = atoi(argv[i]);printf("%12d => %-12d %s!\n", n, reverse(n), reverse(reverse(n))==n ? "passed":"failed");}return 0;}
C++ solution by pezy/LeetCode
#include <climits>class Solution {public:int reverse(int x) {long res = 0;do {res = res*10 + x%10;} while (x /= 10);return res>INT_MAX ? 0 : res;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/reverse-integer/description//// Author : liuyubobobo/// Time : 2018-07-08#include <iostream>using namespace std;/// Using long long to solve the overflow problem/// Time Complexity: O(logx)/// Space Complexity: O(logx)class Solution {public:int reverse(int x) {if(x == 0)return x;int sign = x > 0 ? 1 : -1;long long num = abs((long long)x);long long reverseNum = 0;while(num){reverseNum = reverseNum * 10 + num % 10;num /= 10;}reverseNum *= sign;if(reverseNum > INT_MAX || reverseNum < INT_MIN)return 0;return reverseNum;}};int main() {cout << Solution().reverse(123) << endl;cout << Solution().reverse(-123) << endl;cout << Solution().reverse(12) << endl;cout << Solution().reverse(INT_MAX) << endl;cout << Solution().reverse(INT_MIN) << endl;return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/reverse-integer/description//// Author : liuyubobobo/// Time : 2018-07-08#include <iostream>#include <vector>#include <cassert>using namespace std;/// Using digit vector to solve the overflow problem/// Time Complexity: O(logx)/// Space Complexity: O(logx)class Solution {public:int reverse(int x) {if(x == 0)return x;if(x == INT_MIN)return 0;int sign = x > 0 ? 1 : -1;x = abs(x);vector<int> reverseDigits;while(x){reverseDigits.push_back(x % 10);x /= 10;}if(sign > 0 && overflow(reverseDigits, {2, 1, 4, 7, 4, 8, 3, 6, 4, 7}))return 0;else if(sign < 0 && overflow(reverseDigits, {2, 1, 4, 7, 4, 8, 3, 6, 4, 8}))return 0;return sign * getNumber(reverseDigits);}private:int getNumber(const vector<int>& digits){int res = 0;for(int digit: digits)res = res * 10 + digit;return res;}bool overflow(const vector<int>& digits, const vector<int>& max){if(digits.size() < max.size())return false;assert(digits.size() == max.size());for(int i = 0 ; i < digits.size() ; i ++)if(digits[i] > max[i])return true;else if(digits[i] < max[i])return false;return false;}};int main() {// cout << INT_MAX << endl; // 2147483647// cout << INT_MIN << endl; // -2147483648cout << Solution().reverse(123) << endl; // 321cout << Solution().reverse(-123) << endl; // -321cout << Solution().reverse(12) << endl; // 21cout << Solution().reverse(INT_MAX) << endl; // 0cout << Solution().reverse(INT_MIN) << endl; // 0cout << Solution().reverse(-2147483412) << endl; // -2143847412return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/reverse-integer/description//// Author : liuyubobobo/// Time : 2018-07-08#include <iostream>#include <vector>#include <cassert>using namespace std;/// Poping digit one by one and check before overflow/// Time Complexity: O(logx)/// Space Complexity: O(1)class Solution {public:int reverse(int x) {if(x == 0)return x;if(x == INT_MIN)return 0;int sign = x > 0 ? 1 : -1;x = abs(x);int reverseNum = 0;while(x){if(reverseNum > INT_MAX / 10 || (reverseNum == INT_MAX / 10 && x % 10 > 7))return 0;reverseNum = reverseNum * 10 + x % 10;x /= 10;}return sign * reverseNum;}};int main() {// cout << INT_MAX << endl; // 2147483647// cout << INT_MIN << endl; // -2147483648cout << Solution().reverse(123) << endl; // 321cout << Solution().reverse(-123) << endl; // -321cout << Solution().reverse(12) << endl; // 21cout << Solution().reverse(INT_MAX) << endl; // 0cout << Solution().reverse(INT_MIN) << endl; // 0cout << Solution().reverse(-2147483412) << endl; // -2143847412cout << Solution().reverse(-2147483418) << endl; // 0return 0;}