zoukankan      html  css  js  c++  java
  • [LeetCode]7. 整数反转

    给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。

    示例 1:

    输入: 123
    输出: 321
     示例 2:

    输入: -123
    输出: -321
    示例 3:

    输入: 120
    输出: 21
    注意:

    假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231,  231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/reverse-integer

    C++

    class Solution {
    public:
        int reverse(int x) {
            int res = 0;
            while (x != 0) {
                if (abs(res) > INT_MAX / 10) return 0;
                res = res * 10 + x % 10;
                x /= 10;
            }
            return res;
        }
    };
    abs(res) > INT_MAX / 10 还需研究一下

    C
    int reverse(int x) {
        int res = 0;
        while (x != 0) {
            if (abs(res) > INT_MAX / 10) return 0;
            res = res * 10 + x % 10;
            x /= 10;
        }
        return res;
    }
    参考来源https://www.cnblogs.com/grandyang/
  • 相关阅读:
    hdu 2227
    小A的数学题
    E
    F
    C
    Ping-Pong (Easy Version)的解析
    余数之和BZOJ1257
    大数求余
    数论学习 算法模板(质数,约数)
    Acwing 197. 阶乘分解
  • 原文地址:https://www.cnblogs.com/moonpie-sun/p/9426131.html
Copyright © 2011-2022 走看看