Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
1 |
|
Example 2:
1 |
|
Example 3:
1 |
|
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [$−2^{31}$, $2^{31} − 1$]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Related Topics: Math
解題邏輯與實作
這題是要翻轉整數,如果直接當字串用 slice 就可以輕鬆解決,在加上 python 幾乎沒有 overflow 的問題,所以過程中也不用特地處理,回傳做個判斷就好,就是要注意一下正負問題而已,喔…還有尾數為 0 的狀況…
1 |
|
不過這一題,應該不是希望這樣寫才對,所以又乖乖著墨了一版不轉成字串的版本。
1 |
|