FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/code/lc29.java at master · mJackie/leetcode · GitHub
mJackie
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
135
Star
405
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
code
/
lc29.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
38 lines (38 loc) · 1.36 KB
Breadcrumbs
leetcode
/
code
/
lc29.java
Copy path
File metadata and controls
38 lines (38 loc) · 1.36 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package
code
;
/*
* 29. Divide Two Integers
* 题意:不用乘法,除法,取模运算实现除法
* 难度:Medium
* 分类:Math, Binary Search
* 思路:被除数减去除数,除数每次左移一位,也就是*2 来实现类似二分的思想
* Tips:注意下用long类型,以及溢出的情况,注意符号
*/
public
class
lc29
{
public
static
void
main
(
String
[]
args
) {
System
.
out
.
println
(
divide
(-
1010369383
,-
2147483648
));
}
public
static
int
divide
(
int
dividend
,
int
divisor
) {
if
(
divisor
==
0
||
dividend
==
Integer
.
MIN_VALUE
&&
divisor
==-
1
)
return
Integer
.
MAX_VALUE
;
//溢出的话直接返回最小值
int
sign
;
if
( (
dividend
>
0
&&
divisor
>
0
) || (
dividend
<
0
&&
divisor
<
0
) )
sign
=
1
;
else
sign
= -
1
;
long
dividend2
=
Math
.
abs
((
long
)
dividend
);
//注意这要强制转换成long,再取abs
long
divisor2
=
Math
.
abs
((
long
)
divisor
);
long
div
=
divisor2
;
long
res
=
0
;
//这里也是long
int
p
=
0
;
while
( !(
div
>
dividend2
&&
div
==
divisor2
) ){
long
temp
=
dividend2
-
div
;
if
(
temp
>=
0
){
res
+=
Math
.
pow
(
2
,
p
);
p
++;
div
=
div
<<
1
;
dividend2
=
temp
;
}
else
{
div
=
divisor2
;
p
=
0
;
}
}
return
(
int
)
res
*
sign
;
}
}
Back
|
FazBrowse Home
|
New Git URL