[LeetCode#7]Reverse Integer

來源:互聯網
上載者:User

標籤:

Reverse a integer is a very common task we need to understand for being a good programmer. It is very easy at some aspects, however, it also needs a prudent mind to understand all corner cases it may encounter.

The basic idea of reversing a number is to use mod "%" and divid "/" collectedly.
1. get the last digit of a number (ten as base, decimal numeral system)
digit = num % 10
2. chop off the last digit of a number
num_remain = num / 10

The basic idea is :

int ret = 0while ( num_remain ! = 0 ) {
  digit
= num % 10;
  ret
= ret * 10 + digit;
  num_remain
= num_remain / 10;}

However, there is a big pitfall at here. The problem is that the ret_num could exceed the max number a integer could represent in Java. What‘s more, if the num is a negative number, the symbol of the ret_num could not be predicted. we need to add some fix for the basic structure above. 

1. convert the number into positive form before reversion.

if (num < 0) {  num = num * -1;
  neg_flag
= true; }

2. add proper mechanism to check possible overflow. (note: the ret_num is positive now!) 

 if (ret != 0) {      if ((Integer.MAX_VALUE - digit) / ret < 10 )             return 0;    if (neg_flag == true) {       if (-10 < (Integer.MIN_VALUE + digit) / ret)             return 0;    }  } ret= ret * 10 + digit;                         

The reason is : (when overflow happens)
iff neg_flag = fase, (note: the ret_num is positive now!)
ret * 10 + digit > Integer.MAX_VALUE <=> (Integer.MAX_VALUE - digit) / ret < 10
iff neg_flag = true,
- (ret * 10 + digit) < Integer.MIN_VALUE <=> -10 < (Integer.MIN_VALUE + digit) / ret

Questions: 

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

My answer:

//take care of overflow//both mod and divid can have positive/negative symbolspublic class Solution {   //watch out for all corner cases!!!    public int reverse(int x) {        int ret = 0;        int digit = 0;        boolean neg_flag = false;            if (x < 0) {            neg_flag = true;            x = -1 * x;  //covert to abs(x), and record the symbol of negative or positive.         }                    while (x != 0) {            digit = x % 10; //get the last digit of x                        if (ret != 0) {  //must follow this pattern to check                 if ((Integer.MAX_VALUE - digit) / ret < 10 )                     return 0;                                    if (neg_flag == true) {                    if (-10 < (Integer.MIN_VALUE + digit) / ret)                     // - (ret * 10 + digit) < Integer.MIN_VALUE                    //if we convert the number to abs, we need to compare it in negative form with Integer.MIN_VALUE                   return 0;                }             }                        ret = ret * 10 + digit;            x = x / 10; //chop off the last digit of x        }                if (neg_flag == true && ret > 0)            ret = -1 * ret;                    return ret;    }}

 

[LeetCode#7]Reverse Integer

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.