Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////
- // my solution
- ///////////////////////////////
- int reverse(int x) {
- if (x == 0) return x;
- int xx = x;
- bool minus = false;
- if (xx < 0)
- {
- if (xx==numeric_limits<int>::min()) return 0;
- minus = true;
- xx = -1 * xx;
- }
- long long y = 0;
- while (xx) {
- int d = xx%10;
- y = y*10 + d;
- xx = xx/10;
- }
- if (y < numeric_limits<int>::min()) return 0;
- if (y > numeric_limits<int>::max()) return 0;
- return minus ? -y : y;
- }
- ///////////////////////////////
- // improved
- //////////////////////////////
- int reverse(int x) {
- using LL = long long int;
- LL num = x;
- LL ans = 0;
- while (num)
- {
- ans = (ans*10) + (num%10);
- num = num/10;
- if (ans > numeric_limits<int>::max() ||
- ans < numeric_limits<int>::min() )
- return 0;
- }
- return static_cast<int>(ans);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement