陈中正的网络日志

LeetCode: Reverse Integer

题目

https://oj.leetcode.com/problems/reverse-integer/

分析

循环求解即可。

代码

class Solution
{
public:
    int reverse(int x)
    {
        bool minus = false;
        int res = 0;

        if (x < 0)
        {
            x = -x;
            minus = true;
        }
        while (x != 0)
        {
            int t;
            t = x % 10;
            x = x / 10;
            res = res * 10 + t;
        }
        if (minus)
            res = -res;

        return res;
    }
};

本文章迁移自http://blog.csdn.net/timberwolf_2012/article/details/39157653

Categories:  OJ题解 

« LeetCode: Regular Expression Mathing LeetCode: Median of Two Sorted Arrays »