首页 > 其他 > 详细

[LeetCode] 7. Reverse Integer

时间:2016-08-05 11:30:45      阅读:85      评论:0      收藏:0      [点我收藏+]
#include <stdio.h>
#include <math.h>

// 7. Reverse Integer
// Difficulty: Easy
// Reverse digits of an integer.
// Example1: x = 123, return 321
// Example2: x = -123, return -321
// https://leetcode.com/problems/reverse-integer/

int reverse(int x) {

    // *** METHOD 2
    long y = 0;            // a trick, may be improved
    while (x) {
        y = y * 10 + x % 10;
        x /= 10;
        if (y > pow(2,31) - 1 || y < -1 * pow(2,31)) {
            printf("(2^31) - 1 = %d, -1 * (2^31) = %d, y = %ld\n", (int)pow(2,31) - 1, -1 * (int)pow(2,31), y);
            return 0;    // IMPORTANT: consider range of int
        }
    }
    return y;
}

int main() {
    int x = 1534236469;
    printf("x = %d, reversed x = %d\n", x, reverse(x));
    return 0;
}

 

[LeetCode] 7. Reverse Integer

原文:http://www.cnblogs.com/skyssky/p/5740375.html

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