当前位置:网站首页>Leetcode t29: divide two numbers
Leetcode t29: divide two numbers
2022-07-01 08:17:00 【Fan Qianzhi】
Title Description
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 [ − 2 31 , 2 31 − 1 ] [−2^{31}, 2^{31} − 1] [−231,231−1]. In this question , If the division result overflows , Then return to 2^{31} − 1.
Ideas
Because multiplication cannot be used in questions 、 Division and remainder operations , You can consider using the addition and subtraction method to calculate .
for example , consider 100/7=14… …2, have access to 100 Yes 7 To cut down , Until negative . however , That's inefficient , Consider subtracting multiple 7( such as 2 i 2^i 2i individual 7), To improve computational efficiency .
You can do the following :
100 − 2 3 ∗ 7 = 100 − 56 = 44 100-2^3*7=100-56=44 100−23∗7=100−56=44,
44 − 2 2 ∗ 7 = 44 − 28 = 16 44-2^2*7=44-28=16 44−22∗7=44−28=16,
16 − 2 1 ∗ 7 = 2 16-2^1*7=2 16−21∗7=2,
because 2 < 7 2<7 2<7, So the final remainder is 2, Divisor is 2 1 + 2 2 + 2 3 = 14 2^1+2^2+2^3=14 21+22+23=14.
however , Consider special circumstances :
- The divisor is 0, Direct output 0.
- The divisor is − 2 31 -2^{31} −231, And the divisor is -1, that , According to the meaning , Should return 2 31 − 1 2^{31}-1 231−1
Code
public int divide(int dividend, int divisor) {
if (dividend == 0) {
return 0;
}
if (dividend == Integer.MIN_VALUE && divisor == -1) {
return Integer.MAX_VALUE;
}
boolean negative;
negative = (dividend^divisor) < 0;
long t = Math.abs((long)dividend);
long d = Math.abs((long)divisor);
int result = 0;
for(int i = 31; i >= 0; i--) {
if( (d<<i) <= t ) {
result += (1<<i);
t -= d<<i;
}
}
return negative ? -result : result;// Reverse sign difference
}
边栏推荐
- Lm08 mesh series mesh inversion (fine)
- web254
- uni 热更新
- How to prevent the other party from saying that he has no money after winning the lawsuit?
- Significance and measures of source code encryption
- 网关gateway-88
- Latex formula code
- Chinese font Gan: zi2zi
- Using settoolkit to forge sites to steal user information
- [staff] key number (key number identification position | key number marking list | a major key identification principle | F, C, G position marking ascending | F major key identification principle | B
猜你喜欢

Use threejs simple Web3D effect

Hijacking a user's browser with beef

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system

How to use layui to display the data in the database in the form of tables

手工挖XSS漏洞

How to troubleshoot SharePoint online map network drive failure?
![Thread safety analysis of [concurrent programming JUC] variables](/img/f9/a3604bec6f7e5317dd2c578da73018.jpg)
Thread safety analysis of [concurrent programming JUC] variables

使用 setoolkit 伪造站点窃取用户信息

OJ input and output exercise

postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
随机推荐
Keithley 2100 software 𞓜 Keithley2400 test software ns SourceMeter
軟鍵盤高度報錯
Book of quantitative trading - reading notes of the man who conquers the market
Conception et mise en service du processeur - chapitre 4 tâches pratiques
leetcode T31:下一排列
【入门】提取不重复的整数
Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure
手工挖XSS漏洞
Gdip - hatchbrush pattern table
Comprehensive experiment Li
程序员养生宝典
[getting started] extract non repeating integers
Programmer's regimen
Gdip - hatchBrush图案表
Differential: definition of total differential, partial derivative, gradient
getInputStream() has already been called for this request
[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order
Learn the knowledge you need to know about the communication protocol I2C bus
Provincial election + noi Part VI skills and ideas
Leetcode T29: 两数相除