7.翻转整数

7.Reverse Integer

问题描述:

Reverse digits of an integer.

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

Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer’s last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

知识补充:

整型,长整型

#include //最大值最小值要包含的头文件
INT_MAX,INT_MIN;//最大值,最小值
long l = 0;//通过长整型来判断整型是否溢出

测试代码:

int reverse(int x) {

        long t = 0;
        while(x)
        {
            t = x%10+t*10;
            //cout<
            x = x/10;
        }
        return (tINT_MAX) ? 0 : t;


        }

性能:

7.翻转整数_第1张图片

你可能感兴趣的:(LeetCode,C++)