1. 程式人生 > >LeetCode 7 — Reverse Integer(C++ Java Python)

LeetCode 7 — Reverse Integer(C++ Java Python)

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?

Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).

題目翻譯:

反轉整數。
例1:X = 123,返回321
例2:X = -123,返回-321

你有沒有想過這些呢?
這裡有一些編碼之前要問的好問題。如果你已經考慮了這些,獎勵積分給你!
如果整數的最後一位是0,應該輸出什麼?例如10,100的情況。
你有沒有注意到反轉後的整數可能溢位?假設輸入的是一個32位的整數,則1000000003反轉後溢位。應該如何處理這種情況?
丟擲一個異常?不錯,但如果不允許丟擲異常呢?那麼你必須重新設計函式(即新增一個額外的引數)。