qingcheng奕  

https://oj.leetcode.com/problems/reverse-integer/

一个整数,给反过来,比如123输出321.注意12300的情况,应该输出321,还有-123,是-321.

class Solution {
public:
    int reverse(int x) {
        if(x == 0)
            return 0;
        bool isNegative = false;
        if(x<0)
        {
            isNegative = true;
            x = -x;
        }
        vector<int> num;
        while(x!=0)
        {
            num.push_back( x%10);
            x = x/10;
        }
        int ans = 0;
        for(int i = 0;i<num.size();i++)
        {
            ans = ans * 10;
            ans += num[i];
        
        }
        if(isNegative)
            ans = ans*(-1);
        return ans;
    }
};

 

posted on 2014-06-21 12:42  qingcheng奕  阅读(148)  评论(0编辑  收藏  举报