leetCode 29.Divide Two Integers (两整数相除) 解题思路和方法

原创 2015年07月07日 23:26:53

Divide Two Integers 


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

If it is overflow, return MAX_INT.


思路:这个题算法上不是很难,但是通过率相当低,只有15%,果然,自己在写完之后,各种出错,而且错误不是算法上的错误,是各种边界值没有考虑到,很多溢出错误等。下面是具体代码,有详细注释。

public class Solution {
    public int divide(int dividend, int divisor) {
        long a = dividend;
        long b = divisor;
        if(b == 0 || (a == Integer.MIN_VALUE && b == -1)){//如果被除数为0或溢出
            return Integer.MAX_VALUE;
        }
        boolean isNegative = false;//是否负数
        //将负数转成正数
        if(a < 0 && b > 0){
            isNegative = true;
            a = -a;
        }else if(a > 0 && b < 0){
            isNegative = true;
            b = -b;
        }else if(a < 0 && b < 0){
            a = -a;
            b = -b;
        }
        if(b == 1){
        	return (int) (isNegative?-a:a);
        }
        //如果被除数大,返回0
        if(a < b){
            return 0;
        }
        divisor = (int) b;//如果是负,将转变为正数
        int step = 1;
        while(b < a){//如果b<a,则b = b*2
            step <<= 1;//右移一位,相当于乘于2
            b <<= 1;//b = b*2
        }
        if(b == a){//如果相等,直接返回
            return  isNegative? -step : step;//判断是否为负数
        }
        dividend = (int) (a - (b >> 1));
        step >>= 1;
        int num = step + divide(dividend,divisor);//递归调用
        return isNegative?-num:num;//返回结果,并判断正负
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

[LeetCode-29] Divide Two Integers(两个整数相除,不用乘除取余算术符)

Divide two integers without using multiplication, division and mod operator.If it is overflow, retur...

LeetCode 29 Divide Two Integers(两个整数相除)(*)

翻译不用乘法、除法、取余操作,将两个数相除。如果它溢出了,返回MAX_INT原文Divide two integers without using multiplication, division a...
  • NoMasp
  • NoMasp
  • 2015-11-17 20:04
  • 3625

[Leetcode] 29. Divide Two Integers 解题报告

题目: Divide two integers without using multiplication, division and mod operator. If it is overflo...

【LeetCode-面试算法经典-Java实现】【029-Divide Two Integers(两个整数相除)】

【029-Divide Two Integers(两个整数相除)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Divide two integers without u...

LeetCode OJ 之 Divide Two Integers (两个整数相除)

题目: Divide two integers without using multiplication, division and mod operator. 求两个整数相除的结果,不能使用...

LeetCode 29 Divide Two Integers (C,C++,Java,Python)

Problem: Divide two integers without using multiplication, division and mod operator. If it ...

LeetCode(29)Divide Two Integers

题目内容 Divide two integers without using multiplication, division and mod operator. 题目分析 最容易的想到的办法,是把...

[LeetCode]29.Divide Two Integers

【题目】 Divide two integers without using multiplication, division and mod operator. If it is overfl...

leetcode:29. Divide Two Integers

描述: Divide two integers without using multiplication, division and mod operator.If it is overflow, r...

LeetCode 29. Divide Two Integers

LeetCode 29. Divide Two Integers C++位运算
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)