当前位置:网站首页>【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;
}
}
边栏推荐
- Dynamic analysis and development prospect prediction report of high purity manganese dioxide in the world and China Ⓡ 2022 ~ 2027
- ArrayBuffer
- HMS core helps baby bus show high-quality children's digital content to global developers
- ArcGIS application (XXII) ArcMap loading lidar Las format data
- Reading notes of how the network is connected - understanding the basic concepts of the network (I)
- Target detection -- intensive reading of yolov3 paper
- C language - Introduction - Foundation - syntax - data type (4)
- The map set type is stored in the form of key value pairs, and the iterative traversal is faster than the list set
- How to ensure the uniqueness of ID in distributed environment
- Some points needing attention in PMP learning
猜你喜欢

2022-2028 global elastic strain sensor industry research and trend analysis report
](/img/3f/4d8f4c77d9fde5dd3f53ef890ecfa8.png)
C語言-入門-基礎-語法-[運算符,類型轉換](六)

Clion console output Chinese garbled code

C语言-入门-基础-语法-数据类型(四)

HMS core helps baby bus show high-quality children's digital content to global developers

GoLand environment variable configuration

Relationship and operation of random events
![C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)](/img/89/0f5f4ba07c637b09abe873016cba2d.png)
C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)

Turn: excellent managers focus not on mistakes, but on advantages

What exactly is DAAS data as a service? Don't be misled by other DAAS concepts
随机推荐
What is permission? What is a role? What are users?
Trim leading or trailing characters from strings- Trim leading or trailing characters from a string?
Report on investment analysis and prospect trend prediction of China's MOCVD industry Ⓤ 2022 ~ 2028
awk从入门到入土(7)条件语句
Use Alibaba cloud NPM image acceleration
Reading notes on how to connect the network - hubs, routers and routers (III)
Analysis report on the development status and investment planning of China's modular power supply industry Ⓠ 2022 ~ 2028
C language - Introduction - Foundation - syntax - data type (4)
China battery grade manganese sulfate Market Forecast and strategic consulting report (2022 Edition)
Awk from entry to earth (7) conditional statements
【无标题】转发最小二乘法
Global and Chinese PCB function test scale analysis and development prospect planning report Ⓑ 2022 ~ 2027
Dynamic analysis and development prospect prediction report of high purity manganese dioxide in the world and China Ⓡ 2022 ~ 2027
C語言-入門-基礎-語法-[運算符,類型轉換](六)
上周热点回顾(6.27-7.3)
C language - Introduction - Foundation - syntax - [main function, header file] (II)
awk从入门到入土(14)awk输出重定向
Explain TCP protocol in detail three handshakes and four waves
C语言-入门-基础-语法-数据类型(四)
[C Advanced] file operation (2)