Reverse Integer

Question

Problem Statement

Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer).

Example

Given x = 123, return 321

Given x = -123, return -321

題解

初看這道題覺得先將其轉換爲字符串然後轉置一下就好了,但是仔細一想這種方法存在兩種缺陷,一是負號需要單獨處理,而是轉置後開頭的0也需要處理。另一種方法是將原數字逐個彈出,然後再將彈出的數字組裝爲新數字,乍看以爲需要用到 stack ,實際上卻是 queue… 所以根本不需要輔助資料結構。關於正負號的處理,我最開始是單獨處理的,後來看其他答案時才發現根本就不用分正負考慮。因爲-1 / 10 = 0.

Java

  1. public class Solution {
  2. /**
  3. * @param n the integer to be reversed
  4. * @return the reversed integer
  5. */
  6. public int reverseInteger(int n) {
  7. long result = 0;
  8. while (n != 0) {
  9. result = n % 10 + 10 * result;
  10. n /= 10;
  11. }
  12. if (result < Integer.MIN_VALUE || result > Integer.MAX_VALUE) {
  13. return 0;
  14. }
  15. return (int)result;
  16. }
  17. }

源碼分析

注意 lintcode 和 leetcode 的方法名不一樣。使用 long 型保存中間結果,最後判斷是否溢出。

Reference