Reverse Integer

Reverse Integer

leetcode


    • Title
    • Code

Title

题目链接


  1. Reverse digits of an integer.
  2. Example1: x = 123, return 321
  3. Example2: x = -123, return -321

Code


  1. class Solution {
  2. public:
  3. int reverse(int x) {
  4. // may be overflow when inverse the input
  5. long long base = 1L;
  6. int Max_Int = (int)((base << 31) - 1);
  7. int Min_Int = (int)(0 - (base << 31));
  8. long long res = 0;
  9. while(x != 0) {
  10. res = res * 10 + (x % 10);
  11. x = x / 10;
  12. }
  13. if(res > Max_Int || res < Min_Int) {
  14. return 0;
  15. }
  16. return (int)res;
  17. }
  18. };
+

你可能感兴趣的:(leetcode)