Posted on 2023-03-18 17:21
Uriel 阅读(35)
评论(0) 编辑 收藏 引用 所属分类:
模拟 、
闲来无事重切Leet Code
模拟浏览器的历史记录维护,包括前进、后退、浏览某个url操作(注意如果后退之后浏览某个url,那之前后几步的history就不保存了)
1 #1472
2 #Runtime: 193 ms (Beats 79.49%)
3 #Memory: 16.5 MB (Beats 54.58%)
4
5 class BrowserHistory(object):
6
7 def __init__(self, homepage):
8 """
9 :type homepage: str
10 """
11 self.history = [homepage]
12 self.pos = 0
13
14
15 def visit(self, url):
16 """
17 :type url: str
18 :rtype: None
19 """
20 self.history = self.history[:self.pos+1] + [url]
21 self.pos += 1
22
23
24 def back(self, steps):
25 """
26 :type steps: int
27 :rtype: str
28 """
29 self.pos = max(0, self.pos - steps)
30 return self.history[self.pos]
31
32
33 def forward(self, steps):
34 """
35 :type steps: int
36 :rtype: str
37 """
38 self.pos = min(len(self.history) - 1, self.pos + steps)
39 return self.history[self.pos]
40
41
42 # Your BrowserHistory object will be instantiated and called as such:
43 # obj = BrowserHistory(homepage)
44 # obj.visit(url)
45 # param_2 = obj.back(steps)
46 # param_3 = obj.forward(steps)