天天看點

[LeetCode] Reverse Integer 翻轉整數

Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321

<a href="https://oj.leetcode.com/problems/reverse-integer/">click to show spoilers.</a>

Have you thought about this?

Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Update (2014-11-10):

Test cases had been added to test the overflow behavior.

翻轉數字問題需要注意的就是溢出問題,看了許多網上的解法,由于之前的OJ沒有對溢出進行測試,是以網上很多人的解法沒有處理溢出問題也能通過OJ。現在OJ更新了溢出測試,是以還是要考慮到。為什麼會存在溢出問題呢,我們知道int型的數值範圍是 -2147483648~2147483647, 那麼如果我們要翻轉 1000000009 這個在範圍内的數得到 9000000001,而翻轉後的數就超過了範圍。

我最開始的想法是,用long long 型資料,其數值範圍為 -9223372036854775808~9223372036854775807, 遠大于int型這樣就不會出現溢出問題。代碼如下:

解法一:

送出通過後,OJ給出了官方解答,一看比自己的寫的更精簡一些,它沒有特意處理正負号,仔細一想,果然正負号不影響計算,而且沒有用long long型資料,感覺寫的更好一些,那麼就貼出來吧:

解法二:

在貼出答案的同時,OJ還提了一個問題 To check for overflow/underflow, we could check if ret &gt; 214748364 or ret &lt; –214748364 before multiplying by 10. On the other hand, we do not need to check if ret == 214748364, why? (214748364 即為 INT_MAX / 10)

為什麼不用check是否等于214748364呢,因為輸入的x也是一個整型數,是以x的範圍也應該在 -2147483648~2147483647 之間,那麼x的第一位隻能是1或者2,翻轉之後res的最後一位隻能是1或2,是以res隻能是 2147483641 或 2147483642 都在int的範圍内。但是它們對應的x為 1463847412 和 2463847412,後者超出了數值範圍。是以當過程中res等于 214748364 時, 輸入的x隻能為 1463847412, 翻轉後的結果為 2147483641,都在正确的範圍内,是以不用check。

我們也可以用long long型變量儲存計算結果,最後傳回的時候判斷是否在int傳回内,參見代碼如下:

解法三:

下面這種方法是上面解法二的變形,其實也不難了解,因為都是用int型的,如果超出了範圍,其除以10的結果就不會跟之前的結果一緻,通過這點也可以進行區分,參見代碼如下:

解法四:

繼續閱讀