当前位置:网站首页>【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;
}
}
边栏推荐
- 什么是uid?什么是Auth?什么是验证器?
- UML sequence diagram [easy to understand]
- Reload CUDA and cudnn (for tensorflow and pytorch) [personal sorting summary]
- 2022-2028 global edible probiotic raw material industry research and trend analysis report
- awk从入门到入土(12)awk也可以写脚本,替代shell
- "How to connect the network" reading notes - Web server request and response (4)
- Solve the problem of "Chinese garbled MySQL fields"
- Use Alibaba cloud NPM image acceleration
- At the age of 30, I changed to Hongmeng with a high salary because I did these three things
- Mac platform forgets the root password of MySQL
猜你喜欢

Opencv environment construction (I)
](/img/89/0f5f4ba07c637b09abe873016cba2d.png)
C语言-入门-基础-语法-[标识符,关键字,分号,空格,注释,输入和输出](三)

How should PMP learning ideas be realized?

AI Winter Olympics | is the future coming? Enter the entrance of the meta universe - virtual digital human

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

What exactly is DAAS data as a service? Don't be misled by other DAAS concepts

2022-2028 global intelligent interactive tablet industry research and trend analysis report

Implementation principle of redis string and sorted set

C语言-入门-基础-语法-数据类型(四)
![Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)](/img/3f/4d8f4c77d9fde5dd3f53ef890ecfa8.png)
Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)
随机推荐
Markdown syntax
Analysis report on the development status and investment planning of China's modular power supply industry Ⓠ 2022 ~ 2028
Talk about single case mode
In depth investigation and Strategic Research Report on China's motion controller Market (2022 Edition)
If you can quickly generate a dictionary from two lists
Codeforces Round #793 (Div. 2)(A-D)
Awk from entry to earth (18) GAW K line manual
Dynamic analysis and development prospect prediction report of high purity manganese dioxide in the world and China Ⓡ 2022 ~ 2027
ArcGIS application (XXII) ArcMap loading lidar Las format data
Report on the development trend and prospect trend of high purity zinc antimonide market in the world and China Ⓕ 2022 ~ 2027
《网络是怎么样连接的》读书笔记 - 认识网络基础概念(一)
Awk from entry to earth (15) awk executes external commands
老掉牙的 synchronized 锁优化,一次给你讲清楚!
Flutter 小技巧之 ListView 和 PageView 的各種花式嵌套
Analysis report on the production and marketing demand and investment forecast of tellurium dioxide in the world and China Ⓣ 2022 ~ 2027
地平线 旭日X3 PI (一)首次开机细节
[C Advanced] file operation (2)
如何编写单元测试用例
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
swatch