码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode#7]Reverse Integer

时间:2014-12-30 01:39:15      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

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 = 0
while ( 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 symbols

public 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

标签:

原文地址:http://www.cnblogs.com/airwindow/p/4192732.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!