[每日一题]leetcode 7. 整数反转

class Solution {
public:

    int reverse(int x) {
       // cout << 1111 << endl;
       long long ret = 0;
       long long y = x;
       int f = 0;
       if(y < 0) f = 1, y = -y;
     //  cout << 1111 << endl;
       while(y)
       {
           int tmp = y % 10;
           ret = ret * 10 + tmp;
           y /= 10;
       } 
       if(f) ret = -ret;
       if(ret < INT_MIN || ret > INT_MAX) return 0;
      // if(f) ret = -ret;
      //cout << 111 << endl;
       return (int)ret;

    }
};

 

posted @ 2021-05-03 16:30  WTSRUVF  阅读(37)  评论(0编辑  收藏  举报