四川省建设厅官网站济宁网站建设第一品牌
这里是模拟浏览器的回退和前进
用一个列表来存储浏览器的历史记录
cur指向当前浏览器记录的位置
visit 就是清空之前访问过的历史记录,把当前的放进去
self.cur+=1代表进下一个访问
然后删掉 当前的和之前的,加入新的
前进和回退要考虑边界
class BrowserHistory:def __init__(self, homepage: str):self.history=[homepage]self.cur=0def visit(self, url: str) -> None:self.cur+=1del self.history[self.cur:]self.history.append(url)def back(self, steps: int) -> str:self.cur=max(self.cur-steps,0)return self.history[self.cur]def forward(self, steps: int) -> str:self.cur=min(self.cur+steps,len(self.history)-1)return self.history[self.cur]# Your BrowserHistory object will be instantiated and called as such:
# obj = BrowserHistory(homepage)
# obj.visit(url)
# param_2 = obj.back(steps)
# param_3 = obj.forward(steps)