【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进行移位除法,主要思路是 a = b *(2^31) * x31+ b
*(2^30) * x30 + ... +
b * x0,除法的结果为 x31x30...x0,xi表示32位数字中第i位上的数字(0或者1)

(3)最后通过异或和移位判断结果是否带有负号。

Java AC代码:

public class Solution {

	public int divide(int dividend, int divisor) {
		long a = Math.abs((long) dividend);
		long b = Math.abs((long) divisor);
		long res = 0;
		for (int i = 31; i >= 0; i--) {
			if (a >> i >= b) {
				res += 1 << i;
				a -= b << i;
			}
		}
		return (int) (((dividend ^ divisor) >> 31) == 0 ? (res) : (-res));
	}
}
时间: 2024-10-14 05:02:15

【leetcode】Divide Two Integers的相关文章

【leetcode】Divide Two Integers (middle)☆

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 思路: 尼玛,各种通不过,开始用纯减法,超时了. 然后用递归,溢出了. 再然后终于开窍了,用循环,把被除数每次加倍去找答案,结果一遇到 -2147483648 就各种不行, 主要是这个数一求绝对值就溢出了. 再然后,受不了了,看答案. 发现,大家都用long long来解决溢

【LeetCode】P029_DivideTwoIntegers

[题目] Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 题目的意思就是:不使用*./.%运算符进行除法运算 [思路] 这道题最直接的想法,就是被除数每次减去除数,计算减了多少次,即为所求.但是这个办法的效率太低,试想被除数是Integer.MAX_VALUE(2147483647),除数是1,则要减2147483647次

【LeetCode】数学(共106题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers [7]Reverse Integer [8]String to Integer (atoi) [9]Palindrome Number [12]Integer to Roman [13]Roman to Integer [29]Divide Two Integers [43]Multiply Strings [50]Pow(x,

【LeetCode】二分 binary_search(共58题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [4]Median of Two Sorted Arrays [29]Divide Two Integers [33]Search in Rotated Sorted Array [34]Find First and Last Position of Element in Sorted Array [35]Search Insert Position [50]Pow(

【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】Fraction to Recurring Decimal【Solution】

[题目] Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. For example, Given numerator = 1, denominator = 2,

【LeetCode】 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figu

【LeetCode】Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example:A = 

【LeetCode】Sort Colors

LeetCode OJ Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, w