Divide Two Integers leetcode java

题目

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

题解

这道题我自己没想出来。。。乘除取模都不让用。。那只有加减了。。。我参考的http://blog.csdn.net/perfect8886/article/details/23040143

代码如下:

1      public int divide(int dividend, int divisor) {  
 2         if (dividend == 0 || divisor == 0) {  
 3             return 0;  
 4         }  
 5         boolean isNeg = (dividend > 0 && divisor < 0)  
 6                 || (dividend < 0 && divisor > 0);  
 7         long a = Math.abs((long) dividend);  
 8         long b = Math.abs((long) divisor);  
 9         if (b > a) {  
10             return 0;  
11         }  
12   
13         long sum = 0;  
14         long pow = 0;  
15         int result = 0;  
16         while (a >= b) {  
17             pow = 1;  
18             sum = b;  
19             while (sum + sum <= a) {  
20                 sum += sum;  
21                 pow += pow;  
22             }  
23             a -= sum;  
24             result += pow;  
25         }  
26         return isNeg ? -result : result;  
27     }

Reference:

http://blog.csdn.net/perfect8886/article/details/23040143

时间: 2025-01-02 00:55:05

Divide Two Integers leetcode java的相关文章

Divide Two Integers leetcode

题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 看讨论区大神的思路: In this problem, we are asked to divide two integers. However, we are not allowed to use division, multi

Divide Two Integers —— LeetCode

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 题目大意:不用乘除取模运算计算两个数的除. 解题思路:只能用位运算了,当被除数大于除数,除数左移1位.2位……直到得到最大的然后用被除数减去它,将因数加到res上,这里有点二分的意思,依次循环往复,这里我把两个数都设为负数,因为0x80000000是最小的负数,它没有对应的最

Java for LeetCode 029 Divide Two Integers

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 解题思路: 既然不呢个用乘除和取模运算,只好采用移位运算,可以通过设置一个length代表divisor向左的做大移位数,直到大于dividend,然后对length做一个循环递减,dividend如果大于divisor即进行减法运算,同时result加上对应的值,注意边界条

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

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刷题笔记】Divide Two Integers

Divide two integers without using multiplication, division and mod operator. 题解:要求不用乘除和取模运算实现两个数的除法. 那么用加减法是很自然的选择.不过如果一次只从被除数中剪掉一个除数会TLE.所以我们借助移位运算,依次从被除数中减去1个除数,2个除数,4个除数......当减不动的时候,再依次从被除数中减去......4个除数,2个除数,1个除数. 例如50除以5的计算过程如下: dividend exp tem

[LeetCode] 029. Divide Two Integers (Medium) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 029. Divide Two Integers (Medium) 链接: 题目:https://oj.leetcode.com/problems/divide-two-integers/ 代码(github):https://github.com/illuz/leetcode 题意: 实现除法,不能用乘.除和取模

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). 可以

【leetcode】Divide Two Integers

题目: Divide two integers without using multiplication, division and mod operator. 解析:不使用乘号.除号和余号实现两个整数的除法.该题可以利用移位操作数求解,主要分为三个步骤: (1)先求两个int类型整数的绝对值,注意要将int类型转化成long类型才能求绝对值:因为int类型的范围是-2147483648~2147483647,如果某个数为-2147483648,其绝对值会溢出. (2)对被除数a和除数b进行移位