zoukankan      html  css  js  c++  java
  • [LeetCode] #7 Reverse Integer

    Reverse digits of an integer.

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

    题目很简单,先判断数字的正负,再按位倒置数字就可以了。不过要考虑int的上限。时间:16ms

    代码如下:

    class Solution {
    public:
        int reverse(int x) {
            bool sign = x > 0 ? false : true;
            int temp = x > 0 ? x : 0-x;
            long long y = 0;
            
            while (temp){
                y = y * 10 + temp % 10;
                temp = temp / 10;
            }
            if (y > 2147483647 || (0- y > 2147483648))
                return 0;
            if (sign == true)
                y = 0 - y;
            return y;
        }
    };
    “If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.”
  • 相关阅读:
    关于Unity中资源打包
    关于Unity中的物理
    关于Unity中特殊目录
    关于Unity中脚本
    千八行
    早春游园
    西湖冬景
    七尖行
    黄山游记
    四季
  • 原文地址:https://www.cnblogs.com/Scorpio989/p/4412305.html
Copyright © 2011-2022 走看看