当前位置:网站首页>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
}
边栏推荐
- Office365 - how to use stream app to watch offline files at any time
- 软键盘高度报错
- How to prevent the other party from saying that he has no money after winning the lawsuit?
- 【入门】提取不重复的整数
- Hijacking a user's browser with beef
- [force deduction 10 days SQL introduction] Day10 control flow
- [redis] it takes you through redis installation and connection at one go
- XX attack - reflective XSS attack hijacking user browser
- How outlook puts together messages with the same discussion
- XX攻击——反射型 XSS 攻击劫持用户浏览器
猜你喜欢

图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证

CPU設計實戰-第四章實踐任務一簡單CPU參考設計調試
![[introduction] approximate value](/img/6b/597178d848dd21110f36601fc31092.png)
[introduction] approximate value

Soft keyboard height error

Latex formula code
![[untitled]](/img/d9/5e97f2de256b9749131b5bf1437d24.png)
[untitled]

Embedded-c language-10-enumeration / (function) pointer (function) / multi-level pointer /malloc dynamic allocation / file operation

Differential: definition of total differential, partial derivative, gradient

Use threejs simple Web3D effect

【入门】取近似值
随机推荐
How to use layui to display the data in the database in the form of tables
Set up file server Minio for quick use
Li Kou daily question - day 31 -1790 Can a string exchange be performed only once to make two strings equal
【无标题】
Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure
Cmake I two ways to compile source files
Aardio - [problem] the problem of memory growth during the callback of bass Library
php laravel微信支付
Using settoolkit to forge sites to steal user information
Latex formula code
Uni hot update
Principle and process of embossing
量化交易之读书篇 - 《征服市场的人》读书笔记
Find the nearest n-th power of 2
CPU design practice - Chapter 4 practical tasks - simple CPU reference design and debugging
Programmer's regimen
Access报表实现小计功能
【Redis】一气呵成,带你了解Redis安装与连接
Adding color blocks to Seaborn clustermap matrix
getInputStream() has already been called for this request