当前位置:网站首页>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
}
边栏推荐
- [question brushing] character statistics [0]
- [untitled]
- 7-26 word length (input and output in the loop)
- CPU設計實戰-第四章實踐任務一簡單CPU參考設計調試
- 图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
- Chinese font Gan: zi2zi
- [force deduction 10 days SQL introduction] Day9 control flow
- Lm08 mesh series mesh inversion (fine)
- How outlook puts together messages with the same discussion
- Aardio - Shadow Gradient Text
猜你喜欢

CPU design practice - Chapter 4 practical tasks - simple CPU reference design and debugging

【无标题】
![[untitled]](/img/d9/5e97f2de256b9749131b5bf1437d24.png)
[untitled]

Latex formula code

凸印的印刷原理及工艺介绍

CPU設計實戰-第四章實踐任務一簡單CPU參考設計調試

Aardio - Shadow Gradient Text

P4 安装bmv2 详细教程

SharePoint - how to quickly check whether SharePoint is standard or enterprise edition?

postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
随机推荐
P4 installation bmv2 detailed tutorial
Embedded-c language-10-enumeration / (function) pointer (function) / multi-level pointer /malloc dynamic allocation / file operation
web254
[getting started] extract non repeating integers
SharePoint - modify web application authentication using PowerShell
Learn reptiles for a month and earn 6000 a month? Tell you the truth about the reptile, netizen: I wish I had known it earlier
Connect timed out of database connection
【力扣10天SQL入门】Day10 控制流
[untitled]
Latex table
CPU design practice - Chapter 4 practical tasks - simple CPU reference design and debugging
图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
2022.6.30 省赛+蓝桥国赛记录
[force deduction 10 days SQL introduction] Day9 control flow
Li Kou daily question - day 31 -1790 Can a string exchange be performed only once to make two strings equal
Provincial selection + noi Part II string
【Redis】一气呵成,带你了解Redis安装与连接
[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order
XX attack - reflective XSS attack hijacking user browser
手工挖XSS漏洞