zoukankan      html  css  js  c++  java
  • [LeetCode#29]Divide Two Integers

    Problem:

    Divide two integers without using multiplication, division and mod operator.

    If it is overflow, return MAX_INT.

    Analysis:

    The question is not hard!!! The idea behind this problem is simple, the divident conisited of multi times of divisor.
    dividend = divisor * (2 ^ 0) * a0 + divisor * (2 ^ 1) * a1 + divisor * (2 ^ 2) * a2 + ...
    What we need to compute is a0 ... aN. 
    Note : a << 1 is euqal to a * 2 rather than a ^ 2. (Very important!)
    
    Following the above idea, we could begin to dissect dividend with divisor, but the implementation of this problem involves many skill you should master. 
    1. Since we only care about the how many abs(divisor) needed for abs(dividend), we should covert both integers into to positive first. And use abs should be very careful, since dividend or divisor could be Integer.MIN_VALUE. The method is to covert the Integer into long before use any Math.abs() operation.
    long p_dividend = Math.abs((long)dividend);
    long p_divisor = Math.abs((long)divisor);
    
    Note: (long)dividend is necessary. 
    
    
    2. Use shift operation to improve divisor until the divisor just surpass dividend. 
    int left_move = 0;
    while (p_dividend >= (p_divisor << left_move)) {
        left_move++;
    }
    Note: (p_divisor << left_move) is (1 << left_move) times of p_divisor. 
    Thus, we could get the attribute.
    ret += 1 << (left_move-1); //note, we just surpass divisor, thus left_move need minus 1.
    p_dividend -= (p_divisor << left_move-1); // we need to minus the part that has already been recorded in ret.
    
    
    Note: 
    For overflow situation, we need to return Integer.MAX_VALUE. There is only one overflow situation, we could separte it out.
    if (dividend == Integer.MIN_VALUE && divisor == -1)
        return Integer.MAX_VALUE;

    Solution:

    public class Solution {
        public int divide(int dividend, int divisor) {
            if (divisor == 0) 
                return Integer.MAX_VALUE;
            if (dividend == Integer.MIN_VALUE && divisor == -1)
                return Integer.MAX_VALUE;
            long p_dividend = Math.abs((long)dividend);
            long p_divisor = Math.abs((long)divisor);
            int ret = 0;
            while (p_dividend >= p_divisor) {
                int left_move = 0;
                while (p_dividend >= (p_divisor << left_move)) {
                    left_move++;
                }
                ret += 1 << (left_move-1);
                p_dividend -= (p_divisor << left_move-1);
            }
            if ((dividend < 0 && divisor < 0) || (dividend > 0 && divisor > 0))
                return ret;
            else
                return -1 * ret;
        }
    }
  • 相关阅读:
    架构师养成记--19.netty
    架构师养成记--18.NIO
    架构师养成记--17.disrunptor 多生产者多消费者
    JS计算字符长度、字节数 -- 转
    BootStrap Modal 点击空白时自动关闭
    架构师养成记--16.disruptor并发框架中RingBuffer的使用
    架构师养成记--15.Disruptor并发框架
    架构师养成记--14.重入锁ReentrantLock 和 读写锁 ReentrantReadWriteLock
    049、Java中使用switch判断,不加入break时的操作
    048、Java中使用switch判断
  • 原文地址:https://www.cnblogs.com/airwindow/p/4784813.html
Copyright © 2011-2022 走看看