周赛看到这道题都乐了,感觉对不起他的5分这个分数。感觉主要考察阅读理解。那首先就阅读理解一下
我们只要使用两个变量:
AC!
python
class BrowserHistory(object):
def __init__(self, homepage):
"""
:type homepage: str
"""
self.queue = []
self.queue.append(homepage)
self.curindex = 0
def visit(self, url):
"""
:type url: str
:rtype: None
"""
if self.curindex < (len(self.queue) - 1):
self.queue = self.queue[:self.curindex + 1][::]
self.queue.append(url)
self.curindex = len(self.queue) - 1
def back(self, steps):
"""
:type steps: int
:rtype: str
"""
if self.curindex - steps < 0:
self.curindex = 0
return self.queue[0]
else:
self.curindex -= steps
return self.queue[self.curindex]
def forward(self, steps):
"""
:type steps: int
:rtype: str
"""
if self.curindex + steps > len(self.queue) - 1:
self.curindex = len(self.queue) - 1
return self.queue[(len(self.queue) - 1)]
else:
self.curindex += steps
return self.queue[self.curindex]