FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode-Solution/Python/reach-a-number.py at master · paperfactory/LeetCode-Solution · GitHub
paperfactory
LeetCode-Solution
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode-Solution
/
Python
/
reach-a-number.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
29 lines (25 loc) · 626 Bytes
Breadcrumbs
LeetCode-Solution
/
Python
/
reach-a-number.py
Copy path
File metadata and controls
29 lines (25 loc) · 626 Bytes
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
# Time: O(logn)
# Space: O(1)
import
math
class
Solution
(
object
):
def
reachNumber
(
self
,
target
):
"""
:type target: int
:rtype: int
"""
target
=
abs
(
target
)
k
=
int
(
math
.
ceil
((
-
1
+
math
.
sqrt
(
1
+
8
*
target
))
/
2
))
target
-=
k
*
(
k
+
1
)
/
2
return
k
if
target
%
2
==
0
else
k
+
1
+
k
%
2
class
Solution2
(
object
):
def
reachNumber
(
self
,
target
):
"""
:type target: int
:rtype: int
"""
target
=
abs
(
target
)
k
=
0
while
target
>
0
:
k
+=
1
target
-=
k
return
k
if
target
%
2
==
0
else
k
+
1
+
k
%
2
Back
|
FazBrowse Home
|
New Git URL