FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_01/id_26/Leetcode_50_26.java at master · feixiangcode/algorithm · GitHub
feixiangcode
/
algorithm
Public
forked from
algorithm001/algorithm
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
Week_01
/
id_26
/
Leetcode_50_26.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
106 lines (90 loc) · 2.62 KB
Breadcrumbs
algorithm
/
Week_01
/
id_26
/
Leetcode_50_26.java
Copy path
File metadata and controls
106 lines (90 loc) · 2.62 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Source : https://leetcode.com/problems/powx-n/
// Id : 50
// Author : Fanlu Hai
// Date : 2018-04-17
public
class
PowXN
{
//Time Limit Exceeded, this should be O(n) which means I need O(log(n)) or better.
public
double
myPowVerySlow
(
double
x
,
int
n
) {
double
result
=
x
;
if
(
n
==
0
) {
return
1
;
}
if
(
n
>
0
) {
for
(
int
i
=
1
;
i
<
n
;
i
++) {
result
*=
x
;
}
return
result
;
}
n
= -
n
;
for
(
int
i
=
1
;
i
<
n
;
i
++) {
result
*=
x
;
}
return
1
/
result
;
}
// x pow of n usually means you need to multiple x for n times, we can use dichotomy to reduce it.
public
double
myPowWithOverflowProblem
(
double
x
,
int
n
) {
if
(
n
==
0
) {
return
1
;
}
if
(
n
<
0
) {
//! here is the problem, -2147483648 will cause int overflow as it can't be bigger than 2147483647
n
= -
n
;
x
=
1
/
x
;
}
double
result
=
1
;
double
tmp
=
x
;
while
(
true
) {
if
(
n
==
1
) {
return
result
*
tmp
;
}
if
(
n
%
2
==
1
) {
result
*=
tmp
;
}
n
/=
2
;
tmp
*=
tmp
;
}
}
// x pow of n usually means you need to multiple x for n times, we can use dichotomy to reduce it.
// solved int overflow problem
// did not use recursion
// 82.1 100%
public
double
myPow
(
double
x
,
int
n
) {
boolean
minValue
=
false
;
if
(
n
==
0
||
x
==
1
) {
return
1
;
}
// handle -2147483648 in a not very clean way
if
(
n
==
Integer
.
MIN_VALUE
) {
n
++;
minValue
=
true
;
}
if
(
n
<
0
) {
n
= -
n
;
x
=
1
/
x
;
}
double
result
=
1
;
double
tmp
=
x
;
while
(
true
) {
if
(
n
==
1
) {
if
(
minValue
)
return
result
*
tmp
*
x
;
return
result
*
tmp
;
}
if
(
n
%
2
==
1
) {
result
*=
tmp
;
}
n
/=
2
;
tmp
*=
tmp
;
}
}
public
static
void
main
(
String
[]
args
) {
PowXN
powXN
=
new
PowXN
();
System
.
out
.
println
(
powXN
.
myPow
(
2
,
11
));
System
.
out
.
println
(
powXN
.
myPow
(
2
,
12
));
System
.
out
.
println
(
powXN
.
myPow
(
5
, -
2
));
System
.
out
.
println
(
powXN
.
myPow
(
8
,
4
));
System
.
out
.
println
(
powXN
.
myPow
(
0.9
,
2147483647
));
System
.
out
.
println
(
powXN
.
myPow
(
1
, -
2147483647
));
System
.
out
.
println
(
powXN
.
myPow
(
0.3
, -
2147483648
));
}
}
Back
|
FazBrowse Home
|
New Git URL