LeetCode - Reverse Integer

24.07.2021

小白一枚,文章仅当日记本记录学习进度 ;)

如被浏览,请多多指教,非常感谢大家纠错与建议!

(因留学顺便练习英语,所以用英文笔记,并无他意)

Solution 1 - Python 

step1: Assume we are dealing with an environment which could only stores integers within the 32-bits signed integer range. - Assume the funciton returns 0 if reversed integer overflows.

step 2: Construct the method. The easiest way is to convert int to str, then reverse the sequence of elements. - Be careful if the input is negative int, the result should also be negative.

Python

class Solution(object):
    def reverse(self, x):
        y = str(abs(x))
        scope = 2 ** 31 - 1 if x > 0 else 2 ** 31
        y = y[::-1]
        output = int(y)
        if output > scope:
            return 0
        return output if x > 0 else -output

Java

class Solution {
    public int reverse(int x) {
        int y = Math.abs(x);
        long rev = 0;
        while (y != 0) {
            int rem = y % 10;
            rev = rev * 10 + rem;
            if (rev < Integer.MIN_VALUE || rev > Integer.MAX_VALUE) {
                return 0;
            }
            y /= 10;
        }
        if (x > 0) {
            return (int)rev;
        } else return -(int)rev;
    }
}
class Solution {
    public int reverse(int x) {
        int rev = 0;
        while (x != 0) {
            int pop = x % 10;
            x /= 10;
            // official solution shows two conditions which are not clear to me 
            if (rev > Integer.MAX_VALUE/10 || (rev == Integer.MAX_VALUE / 10 && pop > 7)) return 0;
            if (rev < Integer.MIN_VALUE/10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) return 0;
            rev = rev * 10 + pop;
        }
        return rev;
    }
}

Solution 2 - Python 

Modulus operation 这个方法很容易看到但还不清楚如果自己动手的话怎么去找出发点

class Solution(object):
    def reverse(self, x):
        x_abs = abs(x)
        num = 0
        scope = 2 ** 31 - 1 if x > 0 else 2 ** 31
        while x_abs != 0:
            num = num * 10 + x_abs % 10
            if num > scope:
                return 0
            x_abs //= 10
        return num if x > 0 else -num

1. Interger Overflow 整数溢出

In numerical terms, it means that after incrementing 1 on Integer. MAX_VALUE (2147483647), the returned value will be -2147483648.  An integer of type int in Java can be negative or positive, which means with its 32 bits, we can assign values between -231 (-2147483648) and 231-1 (2147483647)

solution : https://www.baeldung.com/java-overflow-underflow

为什么python不会出现整数溢出?

https://www.zhihu.com/question/28232557
Using a float allows us to represent a much larger range of values than a 32-bit int, but the amount of precision is still fixed. In fact, a computer stores floating point numbers as a pair of fixed-length(binary) integers. One integer represents the string of digits in the value, and the second represent the exponent that keeps track of where the whole part ends and the fractional part begins.

Fortunately, Python has a better solution for large, exact values. A Python int is not a fixed size, but accommodate whatever value it holds. The only limit is the amount of memory the computer has available to it. When the value is small, Python can just use the computer's underlying int representation and operations. When the values gets larger, Python automatically converts to a representation using more bits. Of course, in order to perform operations on larger numbers, Python has to break down the operations into smaller units that the computer hardware is able to handle. Sort of like the way you might do long division by hand. These operations will not be as efficient (they require more steps), but they allow our Python ints to grow to arbitrary size.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值