## Reverse int within the 32-bit signed integer range: \\$[−2^{31}, 2^{31} − 1]\\$ Optimized

LeetCode Problem

Reverse digits of a 32-bit signed integer. When the reversed integer overflows return 0.

Feedback

Optimized from beta code in the original question here. Based on this LeetCode problem.. I’m trying to come up with a good approach that just math operations to iterate a serious of digits using division, almost treating the `int` like a `stack` poping using modulus, and pushing using multiplication. I think there still may be some overflow edge cases I am not catching like `1534236469`, but need some help coming up with how to detect and prevent such cases.

``#include <cassert> #include <climits> #include <cmath> #include <iostream>  class Solution  {     public:         int reverse(int i) {              if(i > INT_MAX || i < INT_MIN) {                 return 0;             }                                         int sign = 1;             if(i < 0) {                  sign = -1;                 i = i*sign;             }              int reversed = 0;             int pop = 0;              while(i > 0) {                       pop = i % 10;                 reversed = reversed*10 + pop;                 i /= 10;             }                                              std::cout << reversed << '\n';              return reversed*sign;         } };  int main() {     Solution s;          assert(s.reverse(1) == 1);     assert(s.reverse(0) == 0);     assert(s.reverse(123) == 321);     assert(s.reverse(120) == 21);     assert(s.reverse(-123) == -321);     assert(s.reverse(1207) == 7021);         assert(s.reverse(1534236469) == 0);     assert(s.reverse(-2147483412) == -2143847412); } ``
Posted on Categories buy elite proxies

## Reverse int within the 32-bit signed integer range: [−2^31, 2^31 − 1]

Problem

Reverse digits of a 32-bit signed integer. When the reversed integer overflows return 0.

Feedback

Looking for any ways I can optimize this with modern c++ features overall. I hope my use of `const` correctness, exception handling, and assertions is implemented well here, please let me know. Is there any way I can use byte operations to reverse the int and keep track of the sign possibly?

Based on the submission feedback from LeetCode, is it safe to say that the time complexity is `O(n)` and space complexity is `O(n)`? If I can reduce the complexity in anyway would love to know! Thanks for the feedback in advance. ``#include <cassert> #include <climits> #include <stdexcept> #include <string>  class Solution  {     public:         int reverse(int i) {             bool is_signed = false;             if(i < 0) { is_signed = true; }              auto i_string = std::to_string(i);              std::string reversed = "";             while(!i_string.empty()) {                 reversed.push_back(i_string.back());                         i_string.pop_back();             }              try {                 i = std::stoi(reversed);             } catch (const std::out_of_range& e) {                 return 0;             }              if(is_signed) { i *= -1; }              return i;         } };  int main() {     Solution s;     assert(s.reverse(1) == 1);     assert(s.reverse(0) == 0);     assert(s.reverse(123) == 321);     assert(s.reverse(120) == 21);     assert(s.reverse(-123) == -321);     assert(s.reverse(1207) == 7021);         assert(s.reverse(INT_MAX) == 0);     assert(s.reverse(INT_MIN) == 0); } ``
Posted on Categories buy elite proxies