LeetCodeOJ_7_Reverse Integer

原创 2015年11月18日 21:37:46

答题链接

题目:

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.

Tags Math
Similar Problems (E) String to Integer (atoi)

分析:

(1)注意int的范围(32位)
min:-2147483648 max:2147483647

代码:

class Solution {
public:
    int reverse(int x) {

        long int y=0;
        while(x!=0)    
        {
            y=y*10+x%10;
            x=x/10;
        }
        if(y<INT_MIN||y>INT_MAX)
           return 0;
        else
           return y;
    }
};

结果:

这里写图片描述

总结:

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【LeetCode7】【Reverse Integer】java中整形正负表示的一些思考

7.Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, r...

LeetCode | 7)Reverse Integer

题目Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 H...

LeetCode 7 — Reverse Integer(C++ Java Python)

题目:http://oj.leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 1...

7.Reverse Integer

题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 c...

Leetcode 7 Reverse Integer

Reverse Integer Total Accepted: 89449 Total Submissions: 373087 Reverse digits of an intege...

leetcode7. Reverse Integer

反转数字 123 ---》321 -123----》-321 上面的两个例子可以写出一个程序 if(x>0){ while(x%10!=0){ ret=10*ret+x%10;   x/=10; ...

【LeetCode 7】 Reverse Integer【E】

给出一个 int 型数据,将其各数位对换,符号保持不变 虽然是一道简单的问题,但是需要注意一个问题,即数据反转之后的越界问题。对于这个问题,本文提供2个思路。

[7] Reverse Integer

翻转一个整数(不包含符号位),如果翻转后这个数字越界则返回0。

[C++]LeetCode 7:Reverse Integer(翻转整数)

Problem: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321...

LeetCode(7)Reverse Integer

题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)