当前位置:网站首页>【leetcode】29. Divide two numbers
【leetcode】29. Divide two numbers
2022-07-04 09:16:00 【Chinese fir sauce_】
subject :
29. Divide two numbers
Given two integers , Divisor dividend And divisor divisor. Divide two numbers , Multiplication is not required 、 Division and mod Operator .
Return dividend dividend Divide by divisor divisor Get the business .
The result of integer division should be truncated (truncate) Its decimal part , for example :truncate(8.345) = 8 as well as truncate(-2.7335) = -2
Example 1:
Input : dividend = 10, divisor = 3
Output : 3
explain : 10/3 = truncate(3.33333..) = truncate(3) = 3
Example 2:
Input : dividend = 7, divisor = -3
Output : -2
explain : 7/-3 = truncate(-2.33333..) = -2
Tips :
Both dividend and divisor are 32 Bit signed integer .
The divisor is not 0.
Suppose our environment can only store 32 Bit signed integer , The value range is [−231, 231 − 1]. In this question , If the division result overflows , Then return to 231 − 1.
class Solution {
/** * Divide two numbers * * @param dividend Divisor * @param divisor Divisor * @return merchant ( Do not include decimals ) */
public static int divide(int dividend, int divisor) {
long result = 0;
long x = dividend;
long y = divisor;
boolean negative = (x < 0 && y > 0) || (x > 0 && y < 0);
if (x < 0) {
x = -x;
}
if (y < 0) {
y = -y;
}
// because x/y The result must be [0,x] Within the scope of , So for x Use dichotomy
long left = 0;
long right = x;
while (left < right) {
long mid = left + right + 1 >> 1;
if (quickMulti(mid, y) <= x) {
// The multiplication result is not greater than x, Move the left pointer to the right
left = mid;
} else {
right = mid - 1;
}
}
result = negative ? -left : left;
// Judge whether it overflows
if (result < Integer.MIN_VALUE || result > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
return (int)result;
}
/** * Fast multiplication * * @param a The multiplier * @param b Multiplier * @return product */
public static long quickMulti(long a, long b) {
long result = 0;
while (b > 0) {
if ((b & 1) == 1) {
// The current minimum is 1, Add in the result a
result += a;
}
// The multiplicand moves to the right 1 position , Equivalent to divided by 2
b >>= 1;
// Multiplier multiplication , Equivalent to times 2
a += a;
}
return result;
}
}
边栏推荐
- Report on investment analysis and prospect trend prediction of China's MOCVD industry Ⓤ 2022 ~ 2028
- 【LeetCode 42】501. Mode in binary search tree
- Flutter tips: various fancy nesting of listview and pageview
- 老掉牙的 synchronized 锁优化,一次给你讲清楚!
- 保姆级JDEC增删改查练习
- HMS core helps baby bus show high-quality children's digital content to global developers
- Démarrage des microservices: passerelle
- Report on research and investment prospect prediction of China's electronic grade sulfuric acid industry (2022 Edition)
- Basic discipline formula and unit conversion
- Use Alibaba cloud NPM image acceleration
猜你喜欢

Industry depression has the advantages of industry depression

165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version

Horizon sunrise X3 PI (I) first boot details
](/img/89/0f5f4ba07c637b09abe873016cba2d.png)
C语言-入门-基础-语法-[标识符,关键字,分号,空格,注释,输入和输出](三)

Codeforces Round #803 (Div. 2)(A-D)

Implementation principle of redis string and sorted set

Getting started with microservices: gateway gateway

GoLand environment variable configuration

地平线 旭日X3 PI (一)首次开机细节

Sequence model
随机推荐
Global and Chinese market of sampler 2022-2028: Research Report on technology, participants, trends, market size and share
C语言-入门-基础-语法-[主函数,头文件](二)
C语言-入门-基础-语法-数据类型(四)
awk从入门到入土(11)awk getline函数详解
Some points needing attention in PMP learning
awk从入门到入土(4)用户自定义变量
How to pass custom object via intent in kotlin
Multilingual Wikipedia website source code development part II
Research Report on the development trend and Prospect of global and Chinese zinc antimonide market Ⓚ 2022 ~ 2027
Investment analysis and prospect prediction report of global and Chinese high purity tin oxide Market Ⓞ 2022 ~ 2027
Sequence model
《网络是怎么样连接的》读书笔记 - 集线器、路由器和路由器(三)
CLion-控制台输出中文乱码
Dynamic analysis and development prospect prediction report of high purity manganese dioxide in the world and China Ⓡ 2022 ~ 2027
awk从入门到入土(9)循环语句
Awk from entry to earth (7) conditional statements
上周热点回顾(6.27-7.3)
Global and Chinese markets of thrombography hemostasis analyzer (TEG) 2022-2028: Research Report on technology, participants, trends, market size and share
《网络是怎么样连接的》读书笔记 - Tcp/IP连接(二)
How to ensure the uniqueness of ID in distributed environment