zoukankan      html  css  js  c++  java
  • LeetCode-Reverse Integer

    Reverse digits of an integer.

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

    Have you thought about this?

    Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

    If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

    Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

    Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).

    这题很神奇,边界都不需要考虑

    class Solution {
    public:
        int reverse(int x) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            if(x/10==0)return x;
            bool flag=x>0;
            if(!flag)x=-x;
            vector<int> digits;
            while(x!=0){
                digits.push_back(x%10);
                x/=10;
            }
            int ret=0;
            
            for(int i=0;i<digits.size();i++){
               ret*=10;
               ret+=digits[i];
               
            }
            if(!flag)ret=-ret;
            return ret;
        }
    };
    
  • 相关阅读:
    199. 二叉树的右视图
    二叉树前、中、后、层次、遍历的非递归法
    奇思妙想
    917. 仅仅反转字母【双指针】
    JVM性能监控与故障处理工具
    Java线程池使用和常用参数(待续)
    MySQL常用知识
    手写常用算法
    LightOj 1170
    逆元总结
  • 原文地址:https://www.cnblogs.com/superzrx/p/3295762.html
Copyright © 2011-2022 走看看