https://leetcode.com/explore/interview/card/top-interview-questions-easy/127/strings/880/
/**
* @param {number} x
* @return {number}
*/
function t1(x) {
let result = parseInt(x.toString().split("").reverse().join(""));
return result < Math.pow(2, 31) - 1 ? result : 0;
}
function t2(x) {
let result = x.toString().split("");
result.push("-");
result.shift();
result.reverse();
result = parseInt(result.join(""));
return result > Math.pow(-2, 31) ? result : 0;
}
var reverse = function (x) {
if (x === 0) return 0;
return (x > 0 ? t1 : t2)(x);
};
GitHub : https://github.com/developeSHG/Algorithm-LeetCode/tree/main/reverse-integer
'Coding Test > LeetCode' 카테고리의 다른 글
[LeetCode] (Trees) Lv Easy. Maximum Depth of Binary Tree (0) | 2023.01.21 |
---|---|
[LeetCode] (Linked List) Lv Easy. Delete Node in a Linked List (0) | 2023.01.16 |
[LeetCode] (Array) Lv Easy. Best Time to Buy and Sell Stock II (0) | 2023.01.16 |
[LeetCode] (Array) Lv Easy. Remove Duplicates from Sorted Array (0) | 2023.01.14 |
[LeetCode] (Array) Lv Easy. Find Target Indices After Sorting Array (0) | 2023.01.12 |
댓글