Files
infloop.life/source/_drafts/leetcode-7-reverse-integer-md.md
2020-07-22 11:22:42 +08:00

67 lines
2.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: leetcode-7-reverse-integer.md
tags: ["leetcode", "solution"]
date: 2019-11-26 20:56:46
---
7. Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [231, 231 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
<!--more-->
题意很简单就是数字反转例如123-\>321。解法也比较简单就是每次取除10的模数然后除10取整原数乘10累加。例如123就是1+10*((0+3)*10+2)=321。需要注意有两个点一个是可能数字是120000那么就不能简单地通过字符串反转来做算数方法做是没问题的另一个就是有边界边界是4字节的有符号整型数字的范围也就是 $$[-2^{-31}, 2^{31}-1]$$。
我的做法比较粗暴判断还有一位的时候用边界去减了除10和当前数比较大小即可其实这么做有问题如果test case长度不是2的31次方位比如100位的数字那也会最后一步才会判断是否溢出对于python这种科学计算型语言不会有问题因为底层已经处理了大数运算。对于C、Java等传统语言这么写就会有问题。当然也可以上来就手动判一下数字长度如果大于10就直接返回0不过我没写。也没想到居然这么写过了代码如
```python
class Solution:
def reverse(self, x: int) -> int:
n = 0
flag = 1
if x < 0:
flag = -1
x = -x
while x > 0:
if x < 10:
if flag == 1 and n > ((1<<31)-1-x)/10 or flag == -1 and n > ((1<<31)-1)/10:
return 0
n = n * 10 + x % 10
x = x // 10
return flag * n
```
然后就是参考题解了。发现有两个地方可以改进代码。一个其实不用一开始就把负数转换为正数因为带符号数累加之后符号还是在的还有一个是判断条件可以做修改为判断为当前数是不是大于MAXINT/10因为如果大于的话那么下一次x10就必然溢出如果等于那么判断尾数是不是大于等于8正数时、小于等于-9负数时
# Refer
[7. Reverse Integer](https://leetcode.com/problems/reverse-integer/)
[7. Reverse Integer](https://leetcode.com/articles/reverse-integer/)
# Appendix