zoukankan      html  css  js  c++  java
  • Reverse Integer

    原题目   https://leetcode.com/problems/reverse-integer/

    Reverse digits of an integer.

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

    click to show spoilers.

    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?

    For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

    Update (2014-11-10):
    Test cases had been added to test the overflow behavior.

    题目大意,反转int数字

    #include <iostream>
    using namespace std;
    
    
    //反转int (利用了long long)
    int reverse(int x) {
        long long s = 0;         
        while (x != 0) {
            s = s * 10 + x %10;
            x = x / 10;
        }
        if (s > INT_MAX || s < INT_MIN)
            return 0;
        return s;
    }
    
    //反正int (没有使用long long)
    int reverse(int x){
        int s = 0;
        while(x != 0) {
            // s * 10 大于Int_Max 或者 s * 10 + x % 10 大于Int_Max
            if (x > 0 && (s > INT_MAX / 10 || (s == INT_MAX / 10 && x % 10 > INT_MAX % 10))) 
                return 0;
            if (x < 0 && (s < INT_MIN / 10 || (s == INT_MIN / 10 && x % 10 < INT_MIN % 10))) 
                return 0;
            s = s * 10 + x % 10;
            x /= 10;
        }
        return s;
    }
    
    void test(){
        int x = 4568412;
        // x = -4568412;
        // x = 1000;
        // x = -1000;
        // x = 0;
        // x = INT_MAX + 1;
        // x = INT_MIN - 1;
        reverse(x);
    }
    
    int main()
    {
        
        test();
    
        system("pause");
        return 0;
    }
  • 相关阅读:
    vue2.0之render函数
    vuex学习笔记
    js小知识点
    vue2.0路由进阶
    vue2.0使用slot插槽分发内容
    js事件机制
    firefox在引入vue.js后不支持e=e||window.event的解决办法
    qs.js使用方法
    除了使用URLSearchParams处理axios发送的数据,但是兼容性不好,其他的兼容方法
    定义设置滚动条
  • 原文地址:https://www.cnblogs.com/the-game-over/p/4704931.html
Copyright © 2011-2022 走看看