TypechoJoeTheme

IT技术分享

统计

[LeetCode 7] Reverse Integer [C] [Runtime : 15 MS]

2017-05-31
/
0 评论
/
653 阅读
/
正在检测是否收录...
05/31

1. Description

Reverse digits of an integer.

2. Example

x = 123, return 321
x = -123, return -321

3. Code

int reverse(int x) {
    long long mResult = 0;
    while (x) {
        mResult = mResult * 10 + x % 10;
        if (mResult > INT_MAX || mResult < INT_MIN) {
            return 0;
        }
        x /= 10;
    }
    return mResult;
}
#include<stdio.h>
#include<limits.h>

int reverse(int x) {
    long long mResult = 0;
    while (x) {
        mResult = mResult * 10 + x % 10;

        if (mResult > INT_MAX || mResult < INT_MIN) {
            return 0;
        }
        x /= 10;
    }
    return mResult;
}

int main() {
    printf("%d\n", reverse(1534236469));
    system("pause");
    return 0;
}

4. Submission Details

5. Runtime Distribution

Digital
朗读
赞 · 0
版权属于:

IT技术分享

本文链接:

https://idunso.com/archives/222/(转载时请注明本文出处及文章链接)