给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。
如果反转后整数超过 32 位的有符号整数的范围 [−231, 231 − 1] ,就返回 0。
假设环境不允许存储 64 位整数(有符号或无符号)。

  1. 输入:x = 123
  2. 输出:321

解题思路

虽然这是一道中等题,单数很简单。最直接的想法就是利用双指针交换最后一位和第一位的数,不过要注意负号。

const reverse = function (x) {
    let strArray = x.toString().split("")
    let end = strArray.length - 1
    let start = strArray[0] === "-" ? 1 : 0
    let temp = ""
    while (start < end) {
        temp = strArray[end]
        strArray[end] = strArray[start]
        strArray[start] = temp
        start++
        end--
    }
    let result = parseInt(strArray.reduce((sum, item) => sum += item, ""))
    return result > Math.pow(2, 31) || result < Math.pow(-2, 31) ? 0 : result
};

go语法复习:go中没有三元表达式,所以只能先声明变量再通过if-else表达式给变量赋值;类型转换string转成int int,err := strconv.Atoi(string); string转成int64 int64,err := strconv.ParseInt(string,10,64) 十进制转int64; int转string string := strconv.Itoa(int); int64转string strconv.FormatInt(int64,10);go中也有math函数

import (
    "math"
    "strconv"
    "strings"
)

func reverse(x int) int {
    arr := strings.Split(strconv.Itoa(x), "")
    end := len(arr) - 1
    var start int
    if arr[0] == "-" {
        start = 1
    } else {
        start = 0
    }
    for start < end {
        temp := arr[end]
        arr[end] = arr[start]
        arr[start] = temp
        end--
        start++
    }
    result := ""
    for _, item := range arr {
        result += item
    }
    num, _ := strconv.Atoi(result)
    if num > int(math.Pow(2, 31)) || num < int(math.Pow(-2, 31)) {
        return 0
    } else {
        return num
    }
}

rust语法复习:类型转换string转为int usize 或者float 类型 parse().unwrap() /parse().unwrap_or(0); 数字类型转换为string调用 to_string()方法 ;数字之间进行转换使用 as 就可以

impl Solution {
    pub fn reverse(x: i32) -> i32 {
        let mut arr: Vec<char> = x.to_string().chars().collect();
        let mut end = arr.len() - 1;
        let mut start = if arr[0] == '-' { 1 } else { 0 };
        while start < end {
            let temp = arr[end];
            arr[end] = arr[start];
            arr[start] = temp;
            end -= 1;
            start += 1;
        }
        let mut result = String::from("");
        arr.iter()
            .enumerate()
            .for_each(|(_, &item)| result.push(item));
        let ans = result.parse().unwrap_or(0);
        ans
    }
}

其他解法

通过数学的方式不断得到最后一位数即取余。再把结果✖️10,不断循环;

const reverse = function (x) {
    let result = 0
    while(x!==0){
        let dis = x % 10
      x = parseInt(x/10)
      result = result*10 + dis
    }
      return result > Math.pow(2,31) || result < Math.pow(-2,31) ? 0 : result
};
func reverse(x int) int {
    result := 0
    for x!=0 {
        dis := x%10
        x = x/10
        result = result*10 + dis
    }
    if result > int(math.Pow(2,31)) || result < int(math.Pow(-2,31)) {
        return 0
    }
    return result
}
impl Solution {
    pub fn reverse(x: i32) -> i32 {
        let mut x = x;
        let mut ans = 0;

        while x != 0 {
            if ans > i32::MAX / 10 || ans < i32::MIN / 10 {
                return 0;
            }
            ans = ans * 10 + x % 10;
            x /= 10;
        }
        ans
    }
}