Difficult:Medium
題目
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 [-2^31, 2^31 - 1], then return 0.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
翻譯
給定一個帶符號的 32 位整數 x,返回 x 的數字反轉。如果反轉 x 導致值超出有符號的 32 位整數範圍 [-2^31, 2^31 - 1],則返回 0。
假設環境不允許您存儲 64 位整數(有符號或無符號)
範例
Example 1
1 | Input: x = 123 |
Example 2
1 | Input: x = -123 |
Example 3
1 | Input: x = 120 |
解題思路
1.將數字變成字串
2.判斷是否為負的整數
3.把負值提出再跑反向迴圈
4.判定是否超出範圍
Solution
Code 1 :
1 | var reverse = function(x) { |
Code 2 :
1 | var reverse = function(x) { |