关闭

LeetCode OJ(7.Reverse Integer)

标签: leetcode
80人阅读 评论(0) 收藏 举报
分类:

题目:
Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321
目前为止做过最简单的一个leetcode题目,解法如下:

class Solution {
public:
    int reverse(int x) {
        long long sum = 0, temp;
        int maxint = 0x7fffffff;
        int minint = 0x80000000;
        if (x >= maxint && x <= minint)
            return 0;
        else
        {
            while (x != 0)
            {
                temp = x % 10;
                sum = sum * 10 + temp;
                sum = sum > maxint ? 0 : sum;
                sum = sum < minint ? 0 : sum;
                x = x / 10;

            }
        }

        return sum;

    }
};
0
0
查看评论

[LeetCode] 007. Reverse Integer (Easy) (C++/Java/Python)

[LeetCode] 007. Reverse Integer (Easy) (C++/Java/Python)
  • hcbbt
  • hcbbt
  • 2015-02-28 00:17
  • 2902

【LeetCode-Algorithm】【7-Reverse Integer】【Python】

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about...
  • IFollowRivers
  • IFollowRivers
  • 2016-09-20 19:04
  • 833

leetcode 7.reverse integer

终于搞懂了第二题,想用C++的long long 类型来实现,但是好像编译器的long long 类型范围和int 一样,所以只能另外想办法了。原来负数也是可以取余的,涨知识了。第二天,加油class Solution { public: int reverse(int x) { ...
  • qq_19332527
  • qq_19332527
  • 2017-10-11 13:02
  • 47

[LeetCode] 7.Reverse Integer

题目描述: https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -3...
  • huaxixidongbeishida
  • huaxixidongbeishida
  • 2015-09-21 20:22
  • 222

leetcode 7.Reverse Integer

//一个test case , 即翻转过来是-2147483648似乎并没有测试到,所以通过了。。。 int reverse(int x){     int sign = x>0?1:-1;     x = x*sign;   ...
  • Zhaohui1995_Yang
  • Zhaohui1995_Yang
  • 2016-06-18 18:31
  • 71

LeetCode 7.Reverse Integer

题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 分析与解答:看似简单的题目其实有两个陷阱,需要注意的是: 1.如果输的数字以0结尾...
  • caishenfans
  • caishenfans
  • 2015-02-02 16:58
  • 222

[LeetCode]7.Reverse Integer

【题目】 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thoug...
  • SunnyYoona
  • SunnyYoona
  • 2013-12-08 15:56
  • 1588

LEETCODE 7.Reverse Integer

leetcode
  • hexiaodouaipiqiu
  • hexiaodouaipiqiu
  • 2016-09-09 00:23
  • 95

【LeetCode】7.Reverse Integer

题目:                     Reverse digits of an integer. ...
  • tmylzq187
  • tmylzq187
  • 2016-03-20 16:08
  • 297

leetcode: 7.Reverse Integer

TopicReverse digits of an integer.ExampleExample1: x = 123, return 321 Example2: x = -123, return -321 Note:The input is assumed to be a 32-bit signed...
  • JNingWei
  • JNingWei
  • 2017-10-30 21:49
  • 198
    个人资料
    • 访问:111321次
    • 积分:3375
    • 等级:
    • 排名:第11800名
    • 原创:223篇
    • 转载:15篇
    • 译文:2篇
    • 评论:19条
    最新评论