首页 > 其他 > 详细

Reverse Integer

时间:2015-04-20 22:05:26      阅读:186      评论:0      收藏:0      [点我收藏+]

Reverse Integer 

Reverse digits of an integer.

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

题目比较简单,注意越界和负数情况即可!

 Runtime: 16 ms

#include <stdio.h>
#include <stdlib.h>

class Solution {
public:
    int reverse(int x) {
        long long  num = 0;
        bool isPositive = true;
        if (x < 0){
            isPositive = false;
            x = -x;
        }
        while (x > 0){
            int temp = x % 10;
            num = num * 10 + temp;
            x = x / 10;
        }
        if (num > INT_MAX){
            return 0;
        }
        else{
            if (isPositive == false)
                num = num*-1;
            return num;
        }
    }
};

int main(){
    Solution solution;
    printf("%d", solution.reverse(-123));
    system("pause");
    return 0;
}

 

Reverse Integer

原文:http://www.cnblogs.com/JeromeHuang/p/4442551.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!