LeetCode-第七题:Reverse Integer

题目

leetCode

完整程序

    #include<stdio.h>
    #include<limits.h> 
    int reverse(int x); 
    int main()
    {
        printf("%d\n",INT_MAX);
        reverse(1534236469);
        return 0;
    }

    int reverse(int x) 
    {
        /*
        *要考虑数字转换过后上溢的情况 
        */
        int num=x>0?x:-x;

        int total=0;
        int remainder;
        while(num!=0)
        {
            remainder=num%10;
            num=num/10;
            if((INT_MAX-remainder)/10<total)
            {
                return 0;//溢出返回0; 
            }
            printf("%d,remainder=%d,num=%d,total=%d\n",964632435*10,remainder,num,total);
        }    
        if(x<0)
            total=-1*total;
        printf("%d\n",total);
        return total;
    }

后记

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容