[leedcode 29] Divide Two Integers

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

If it is overflow, return MAX_INT

public class Solution {
    //本题很多细节需要考虑:
    //1.负数问题,见代码,很讨巧
    //2.因为除法可以转换成减法,如果每次减一次除数,非常耗时间,例如123456/1,需要减123456次
    //因此本题借鉴了位运算,先把除数每次乘以2,直到除数大于被除数,然后再相减,注意此时结果也是成倍增长的
    //然后更新被除数,并且除数不断除以2,累加结果。。。
    //注意终止条件:最终的差要小于原始的除数。
    //本题针对除数和被除数差距很大的情况,节约了大量时间
    //还有一个细节:注意溢出问题,所以需要把所有中间变量都声明为long型!
    public int divide(int dividend, int divisor) {
       long ndividend=Math.abs((long)dividend);
       long ndivisor=Math.abs((long)divisor);
       int flag=1;
       if(dividend<0) flag=-flag;
       if(divisor<0) flag=-flag;
       long result=0;
       long res=1;
       while(ndividend>ndivisor){
           ndivisor=ndivisor<<1;
           res=res<<1;
       }
       while(ndividend>=Math.abs((long)divisor)){
            if(ndividend>=ndivisor){
                ndividend=ndividend-ndivisor;
                result+=res;
            }
            ndivisor=ndivisor>>1;
            res=res>>1;
       }
       if(flag>0){
           if(result>=0x7fffffff) return Integer.MAX_VALUE;
            else return (int)result;
       }
        else return -(int)result;
    }
}
时间: 2024-11-01 05:49:04

[leedcode 29] Divide Two Integers的相关文章

LeetCode --- 29. Divide Two Integers

题目链接:Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 这道题的要求是在不使用乘法.除法.取模运算的前提下实现两个整数相除.如果溢出,返回MAX_INT. 这道题的直接思路是用被除数不断减去除数,直到为0.这种方法的迭代次数是结果的大小,即比如结果为n,算法复杂度是O(n). 可以

No.29 Divide Two Integers

No.29 Divide Two Integers Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 反转数字:将对10整除和取余的结果逆置即可难点即细节:溢出.符号疑问:溢出的,怎么处理?——提交一次出错后,知道应该是返回0 为防溢出,提升类型为long long,注意判断INT_MAX.INT_MIN 1 #include "stdafx.h" 2 #

LeetCode开心刷题十六天——29. Divide Two Integers*

From now on,I grade the questions I've done,* less means more difficult *** done by myself **need see answer,but I can reappear it *need see answer&hard to reappear 29. Divide Two Integers Medium 7003349FavoriteShare Given two integers dividend and d

[Leetcode][Python]29: Divide Two Integers

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 29: Divide Two Integershttps://oj.leetcode.com/problems/divide-two-integers/ Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT. ===C

29. Divide Two Integers

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. (1) log int divide(int dividend, int divisor) { if(dividend == 0) return 0; if(divisor == 0) return INT_MAX; double t1 = log(fabs(dividend

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

翻译 不用乘法.除法.取余操作,将两个数相除. 如果它溢出了,返回MAX_INT 原文 Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 代码 一心扑到了递归上,可惜没能写出来----烦躁至极还是找了别人的答案-- class Solution { public: int divide(int dividend, int d

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

Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 思路:这个题算法上不是很难,但是通过率相当低,只有15%,果然,自己在写完之后,各种出错,而且错误不是算法上的错误,是各种边界值没有考虑到,很多溢出错误等.下面是具体代码,有详细注释. public class Solution { p

Java [leetcode 29]Divide Two Integers

题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 解题思路: 把除数表示为:dividend = 2^i * divisor + 2^(i-1) * divisor + ... + 2^0 * divisor.这样一来,我们所求的商就是各系数之和了,而每个系数都可以通过移位操作获得. 详细解说请参考:http:/

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

Problem: Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. Solution: 不能乘除就加减就行了,但是一个问题是加减有可能速度太慢,因此需要转换,由于任何一个数都能表示成二进制,所以有dividend=divisor*(a*2^1 + b*2^2 + ...... + m*2^k) 所以只要计算出所有diviso