Skip to content

Files

Latest commit

9324b58 · Jun 15, 2023

History

History
231 lines (179 loc) · 5.65 KB

File metadata and controls

231 lines (179 loc) · 5.65 KB

English Version

题目描述

给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。

如果反转后整数超过 32 位的有符号整数的范围 [−231,  231 − 1] ,就返回 0。

假设环境不允许存储 64 位整数(有符号或无符号)。

 

示例 1:

输入:x = 123
输出:321

示例 2:

输入:x = -123
输出:-321

示例 3:

输入:x = 120
输出:21

示例 4:

输入:x = 0
输出:0

 

提示:

  • -231 <= x <= 231 - 1

解法

方法一:数学

我们不妨记 m i m x 分别为 2 31 2 31 1 ,则 x 的反转结果 a n s 需要满足 m i a n s m x

我们可以通过不断地对 x 取余来获取 x 的最后一位数字 y ,并将 y 添加到 a n s 的末尾。在添加 y 之前,我们需要判断 a n s 是否溢出。即判断 a n s × 10 + y 是否在 [ m i , m x ] 的范围内。

x > 0 ,那么需要满足 a n s × 10 + y m x ,即 a n s × 10 + y m x 10 × 10 + 7 。整理得 ( a n s m x 10 ) × 10 7 y

下面我们讨论上述不等式成立的条件:

  • a n s < m x 10 时,不等式显然成立;
  • a n s = m x 10 时,不等式成立的充要条件是 y 7 。如果 a n s = m x 10 并且还能继续添加数字,说明此时数字是最高位,即此时 y 一定不超过 2 ,因此,不等式一定成立;
  • a n s > m x 10 时,不等式显然不成立。

综上,当 x > 0 时,不等式成立的充要条件是 a n s m x 10

同理,当 x < 0 时,不等式成立的充要条件是 a n s m i 10

因此,我们可以通过判断 a n s 是否在 [ m i 10 , m x 10 ] 的范围内来判断 a n s 是否溢出。若溢出,则返回 0 。否则,将 y 添加到 a n s 的末尾,然后将 x 的最后一位数字去除,即 x x 10

时间复杂度 O ( log | x | ) ,其中 | x | x 的绝对值。空间复杂度 O ( 1 )

Python3

class Solution:
    def reverse(self, x: int) -> int:
        ans = 0
        mi, mx = -(2**31), 2**31 - 1
        while x:
            if ans < mi // 10 + 1 or ans > mx // 10:
                return 0
            y = x % 10
            if x < 0 and y > 0:
                y -= 10
            ans = ans * 10 + y
            x = (x - y) // 10
        return ans

Java

class Solution {
    public int reverse(int x) {
        int ans = 0;
        for (; x != 0; x /= 10) {
            if (ans < Integer.MIN_VALUE / 10 || ans > Integer.MAX_VALUE / 10) {
                return 0;
            }
            ans = ans * 10 + x % 10;
        }
        return ans;
    }
}

C++

class Solution {
public:
    int reverse(int x) {
        int ans = 0;
        for (; x; x /= 10) {
            if (ans < INT_MIN / 10 || ans > INT_MAX / 10) {
                return 0;
            }
            ans = ans * 10 + x % 10;
        }
        return ans;
    }
};

Go

func reverse(x int) (ans int) {
	for ; x != 0; x /= 10 {
		if ans < math.MinInt32/10 || ans > math.MaxInt32/10 {
			return 0
		}
		ans = ans*10 + x%10
	}
	return
}

JavaScript

/**
 * @param {number} x
 * @return {number}
 */
var reverse = function (x) {
    const mi = -(2 ** 31);
    const mx = 2 ** 31 - 1;
    let ans = 0;
    for (; x != 0; x = ~~(x / 10)) {
        if (ans < ~~(mi / 10) || ans > ~~(mx / 10)) {
            return 0;
        }
        ans = ans * 10 + (x % 10);
    }
    return ans;
};

C

int reverse(int x) {
    int ans = 0;
    for (; x != 0; x /= 10) {
        if (ans > INT_MAX / 10 || ans < INT_MIN / 10) {
            return 0;
        }
        ans = ans * 10 + x % 10;
    }
    return ans;
}

Rust

impl Solution {
    pub fn reverse(mut x: i32) -> i32 {
        let is_minus = x < 0;
        match x
            .abs()
            .to_string()
            .chars()
            .rev()
            .collect::<String>()
            .parse::<i32>()
        {
            Ok(x) => x * if is_minus { -1 } else { 1 },
            Err(_) => 0,
        }
    }
}

C#

public class Solution {
    public int Reverse(int x) {
        int ans = 0;
        for (; x != 0; x /= 10) {
            if (ans < int.MinValue / 10 || ans > int.MaxValue / 10) {
                return 0;
            }
            ans = ans * 10 + x % 10;
        }
        return ans;
    }
}

...