列表

详情


NC57. 反转数字

描述

给定一个32位的有符号整数num,将num中的数字部分反转,最后返回反转的结果
1.只反转数字部分,符号位部分不反转
2.反转后整数num超过 32 位的有符号整数的范围 [−231,  231 − 1] ,返回 0
3.假设本题不允许存储 64 位整数(有符号或无符号,即C++不能使用long long ,Java不能使用long等)

数据范围:
-231 <= x <= 231-1

示例1

输入:

12

输出:

21

示例2

输入:

-123

输出:

-321

示例3

输入:

10

输出:

1

示例4

输入:

1147483649

输出:

0

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2021-07-17

class Solution {
public:
    /**
     * 
     * @param x int整型 
     * @return int整型
     */
    int reverse(int x) {
        // write code here
//         long long num = x<0? -x : x;
//         string rx = to_string(num);
//         ::reverse(rx.begin(),rx.end());
//         num = stoll(rx);
//         if(x < 0) num = -num;
//         if(num < INT_MIN || num > INT_MAX) return 0;
//         return num;
        long long res = 0;
        while(x != 0){
            int t = x%10;
            res = res * 10 + t;
            x /= 10;
        }
        if(res < INT_MIN || res > INT_MAX) return 0;
        return res;
    }
};

C++ 解法, 执行用时: 2ms, 内存消耗: 380KB, 提交时间: 2021-07-20

class Solution {
public:
    /**
     * 
     * @param x int整型 
     * @return int整型
     */
    int reverse(int x) {
        int num=0,res=0;
        while(x!=0){
            num=x%10;
            if(res>INT_MAX/10||res==INT_MAX/10&&num>INT_MAX%10) return 0;
            if(res<INT_MIN/10||res==INT_MIN/10&&num<INT_MIN%10) return 0;
            
            res=res*10+num;
            x/=10;
        }
        return res;
        
        /*
        int res=0,temp=0;
        while(x){
            if(res>INT_MAX/10||res==INT_MAX&&temp>INT_MAX%10) return 0;
            if(res<INT_MIN/10||res==INT_MIN&&temp<INT_MIN%10) return 0;
            temp=x%10;
            res=res*10+temp;
            x/=10;
        }
        return res;
        */
        
        
        /*
        int temp = 0,res=0;       
        while(x){
            temp = x%10;
            if(res>INT_MAX/10||res==INT_MAX/10&&temp>INT_MAX%10) return 0;
            if(res<INT_MIN/10||res==INT_MIN/10&&temp<INT_MIN%10) return 0;
            res=res*10+temp;
            x/=10;
        }
        return res;
        */
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        /*
        int num = 0,res = 0;
        while(x){
            num=x%10;
            if((res>INT_MAX/10)||res==INT_MAX/10&&num>INT_MAX%10)
                return 0;
            if((res<INT_MIN/10)||res==INT_MIN/10&&num<INT_MIN%10)
                return 0;
            
            res=res*10+num;
            x/=10;
            
        }
        return res;*/
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        /*
        // write code here
        int res=0;
        while(x!=0){
            int pop = x%10;
            x/=10;
            if(x>INT_MAX/10||x==INT_MAX/10&&pop>7)return 0;
            if(x<INT_MIN/10||x==INT_MIN/10&&pop<-8)return 0;
            res = res*10+pop;
        }
        return res;*/
    }
};

C++ 解法, 执行用时: 2ms, 内存消耗: 388KB, 提交时间: 2021-07-20

class Solution {
public:
    /**
     * 
     * @param x int整型 
     * @return int整型
     */
    int reverse(int x) {
        // write code here
        // write code here
        if(x==0 ) return 0;
        int sign=x/abs(x);
        queue<int> s;
        while(abs(x)>0){
//             int j=x%10;
            s.push(abs(x%10));
            x=x/10;
        }
         
        while(!s.empty()){
            x+=s.front();
            x*=10;
            s.pop();
        }
        if(x<0){
            return 0;
        }else{
            return x/10*sign;
        }
    }
};

C++ 解法, 执行用时: 2ms, 内存消耗: 396KB, 提交时间: 2021-09-12

class Solution {
public:
    /**
     * 
     * @param x int整型 
     * @return int整型
     */
    int reverse(int x) {
        // write code here
        long sum=0;
        int sr=x;
        vector<int> temp;
        bool flag=true;
        if(x<0)
            flag=false;
        x=abs(x);
        while(x)
        {
            int data=x%10;
            temp.push_back(data);
            x=x/10;
        }

        for(int i=0;i<temp.size();i++)
        {
            sum=sum*pow(10,1)+temp[i];
        }
        if(flag==false)
        {
            sum=-sum;
        }
        int max=pow(2,31)-1;
        int min=-pow(2,31);
        if(sum>max||sum<min)
            return 0;
        else
            return sum;
    }
};

C++ 解法, 执行用时: 2ms, 内存消耗: 396KB, 提交时间: 2021-08-27

class Solution {
public:
    /**
     * 
     * @param x int整型 
     * @return int整型
     */
    int reverse(int x) {
        // write code here
        long t=0;
        while(x)
        {
            t=10*t+x%10;
            x=x/10;
        }
       
        return (t>INT_MAX ||t<INT_MIN)?0:t;
    }
};

上一题