-
[Leetcode] 7. Reverse Integer TypescriptAlgorithm 2022. 8. 25. 14:57728x90
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).
Example 1:
Input: x = 123 Output: 321
Example 2:
Input: x = -123 Output: -321
Example 3:
Input: x = 120 Output: 21
Constraints:
- -231 <= x <= 231 - 1
function reverse(x: number): number { let isNegative = false; let inputNumber = x; if (x < 0) { isNegative = true; inputNumber = -x; } const reversedNumber = Number(inputNumber.toString().split('').reverse().join('')); if (reversedNumber > Math.pow(2, 31) || x < Math.pow(-2, 31) - 1) { return 0; } return isNegative ? -reversedNumber : reversedNumber; }
728x90'Algorithm' 카테고리의 다른 글
[LeetCode] 11. Container With Most Water (JAVA) (0) 2023.08.26 [Leetcode] 8. String To Integer(atoi) Typescript (0) 2023.01.24 [Leetcode] 6. Zigzag Conversion Typescript (0) 2022.08.25 [Leetcode] 5. Longest palindromic substring (Typescript) (0) 2022.02.01 [leetcode] 4. Median of Two Sorted Arrays (Typescript) (0) 2021.02.21