004-Reverse-integer[整数反转]
# 题目
Given a string s, find the length of the longest substring without repeating characters.
# example
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
1
2
3
2
3
Input: x = 123
Output: 321
1
2
2
Input: x = -123
Output: -321
1
2
2
Input: x = 120
Output: 21
1
2
2
# 我的思路
直接摆烂字符串反转就完事
function reverse(x: number): number {
const flag: boolean = x > 0;
const reverseValue = String(Math.abs(x)).split("").reverse().join("");
if ((Number(reverseValue) < Math.pow(-2, 31)) || (Number(reverseValue) > Math.pow(2, 31) - 1)) {
return 0;
}
return flag ? +reverseValue : -reverseValue;
};
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 最优解
感觉 不如我这个
上次更新: 2022/03/10, 13:31:01