LeedCode Reverse Integer

来源:互联网 发布:淘宝纸箱用什么机器 编辑:程序博客网 时间:2024/05/28 06:04

网址:https://leetcode.com/problems/reverse-integer/description/

题目:

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

click to show spoilers.

Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.


翻译:一个数字反过来输出。


思路;

    易错点是没有做溢出检查。

代码:

class Solution {public:    int reverse(int x) {        int y=0;        int n;        while(x!=0)        {            n=x%10;            if(y>INT_MAX/10||y<INT_MIN/10)//检查溢出            {                return 0;            }            y=y*10+n;            x=x/10;        }        return y;    }};


原创粉丝点击