Skip to content

Commit 94fea23

Browse files
authored
Create design-browser-history.py
1 parent c163adf commit 94fea23

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Python/design-browser-history.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Time: ctor : O(1)
2+
# visit : O(n)
3+
# back : O(1)
4+
# foward: O(1)
5+
# Space: O(n)
6+
7+
class BrowserHistory(object):
8+
9+
def __init__(self, homepage):
10+
"""
11+
:type homepage: str
12+
"""
13+
self.__history = [homepage]
14+
self.__curr = 0
15+
16+
def visit(self, url):
17+
"""
18+
:type url: str
19+
:rtype: None
20+
"""
21+
while len(self.__history) > self.__curr+1:
22+
self.__history.pop()
23+
self.__history.append(url)
24+
self.__curr += 1
25+
26+
def back(self, steps):
27+
"""
28+
:type steps: int
29+
:rtype: str
30+
"""
31+
self.__curr = max(self.__curr-steps, 0)
32+
return self.__history[self.__curr]
33+
34+
def forward(self, steps):
35+
"""
36+
:type steps: int
37+
:rtype: str
38+
"""
39+
self.__curr = min(self.__curr+steps, len(self.__history)-1)
40+
return self.__history[self.__curr]

0 commit comments

Comments
 (0)