forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesign-browser-history.py
40 lines (35 loc) · 926 Bytes
/
design-browser-history.py
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
# Time: ctor : O(1)
# visit : O(n)
# back : O(1)
# foward: O(1)
# Space: O(n)
class BrowserHistory(object):
def __init__(self, homepage):
"""
:type homepage: str
"""
self.__history = [homepage]
self.__curr = 0
def visit(self, url):
"""
:type url: str
:rtype: None
"""
while len(self.__history) > self.__curr+1:
self.__history.pop()
self.__history.append(url)
self.__curr += 1
def back(self, steps):
"""
:type steps: int
:rtype: str
"""
self.__curr = max(self.__curr-steps, 0)
return self.__history[self.__curr]
def forward(self, steps):
"""
:type steps: int
:rtype: str
"""
self.__curr = min(self.__curr+steps, len(self.__history)-1)
return self.__history[self.__curr]