1. 程式人生 > >LeetCode7 給定一個 32 位有符號整數,將整數中的數字進行反轉。

LeetCode7 給定一個 32 位有符號整數,將整數中的數字進行反轉。

class Solution {
public:
    int reverse(int x) {
long long x1 = abs(x);
        long long y=0, n=0;
        std::vector<int> i;
        while(x1/(long long)(pow(double(10),int(n))))
        {
            i.push_back(x1%(long long)(pow(double(10),int(n+1)))/(long long)(pow(double(10),int(n))));
            ++n;
        }  
        for
(int j = 0; j < i.size(); ++j) { auto d = (long long)(pow(double(10),int(i.size()-j-1))); auto b = (long long)(i[j]); y += b*d; } if(x < 0) { y = -y; } if(y < (-pow(double(2),31)) || y > (pow(double
(2),31)-1)) return 0; return y; } };