-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1472.py
67 lines (57 loc) · 1.76 KB
/
1472.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
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
# https://leetcode.com/problems/design-browser-history/
class BrowserHistory:
def __init__(self, homepage: str):
self.back_history = []
self.forward_history = []
self.current = homepage
def visit(self, url: str) -> None:
if self.current:
self.back_history.append(self.current)
self.forward_history.clear()
self.current = url
def back(self, steps: int) -> str:
for _ in range(steps):
if len(self.back_history) > 0:
url = self.back_history.pop()
if self.current:
self.forward_history.append(self.current)
self.current = url
else:
break
return self.current
def forward(self, steps: int) -> str:
for _ in range(steps):
if len(self.forward_history) > 0:
url = self.forward_history.pop()
if self.current:
self.back_history.append(self.current)
self.current = url
else:
break
return self.current
b = BrowserHistory('monm.com')
print(b.visit('bx.com'))
print(b.visit('bjyfmln.com'))
print(b.back(3))
print(b.visit('ijtrqk.com'))
print(b.visit('dft.com'))
print(b.back(10))
print(b.forward(10))
print(b.visit('yc.com'))
print(b.visit('yhl.com'))
print(b.visit('xynxvix.com'))
print(b.visit('izfscdv.com'))
print(b.visit('cdenhm.com'))
print(b.visit('ocgcjz.com'))
print(b.forward(5))
print(b.forward(5))
print(b.visit('gtd.com'))
print(b.back(9))
print(b.visit('hfeour.com'))
print(b.visit('ghmh.com'))
print(b.visit('nnm.com'))
print(b.visit('knm.com'))
print(b.forward(4))
print(b.visit('cbtg.com'))
print(b.visit('acywod.com'))
print(b.visit('mydr.com'))