• [LeetCode#7]Reverse Integer


    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;
        }
    }
  • 相关阅读:
    ONNX 开始
    Linux 性能相关工具
    Prometheus + Grafana 快速上手
    RTSP 流相关工具介绍
    TensorFlow Serving
    TensorFlow 的 JupyterLab 环境
    【排序算法动画解】排序介绍及冒泡排序
    上课老师提问我什么是二叉查找树,我把这些动图拿了出来,动图图解及代码实现。
    如何找东西?查找算法之顺序查找和二分查找详解
    【数据结构和算法:简单方法】谈一谈优先队列的实现
  • 原文地址:https://www.cnblogs.com/airwindow/p/4192732.html
Copyright © 2020-2023  润新知