class BrowserHistory:
def __init__(self, homepage: str):
self.back_history = [homepage]
self.front_history = []
def visit(self, url: str) -> None:
self.back_history.append(url)
self.front_history = []
def back(self, steps: int) -> str:
while steps and len(self.back_history) > 1:
self.front_history.append(self.back_history.pop())
steps -= 1
return self.back_history[-1]
def forward(self, steps: int) -> str:
while steps and self.front_history:
self.back_history.append(self.front_history.pop())
steps -= 1
return self.back_history[-1]Where is the number of visited urls, is the average length of each url, and is the number of steps we go forward or back.
class BrowserHistory:
def __init__(self, homepage: str):
self.history = [homepage]
self.cur = 0
def visit(self, url: str) -> None:
self.cur += 1
self.history = self.history[:self.cur]
self.history.append(url)
def back(self, steps: int) -> str:
self.cur = max(0, self.cur - steps)
return self.history[self.cur]
def forward(self, steps: int) -> str:
self.cur = min(len(self.history) - 1, self.cur + steps)
return self.history[self.cur]Where is the number of visited urls and is the average length of each url.
class BrowserHistory:
def __init__(self, homepage: str):
self.history = [homepage]
self.cur = 0
self.n = 1
def visit(self, url: str) -> None:
self.cur += 1
if self.cur == len(self.history):
self.history.append(url)
self.n += 1
else:
self.history[self.cur] = url
self.n = self.cur + 1
def back(self, steps: int) -> str:
self.cur = max(0, self.cur - steps)
return self.history[self.cur]
def forward(self, steps: int) -> str:
self.cur = min(self.n - 1, self.cur + steps)
return self.history[self.cur]Where is the number of visited urls and is the average length of each url.
class ListNode:
def __init__(self, val, prev=None, next=None):
self.val = val
self.prev = prev
self.next = next
class BrowserHistory:
def __init__(self, homepage: str):
self.cur = ListNode(homepage)
def visit(self, url: str) -> None:
self.cur.next = ListNode(url, self.cur)
self.cur = self.cur.next
def back(self, steps: int) -> str:
while self.cur.prev and steps > 0:
self.cur = self.cur.prev
steps -= 1
return self.cur.val
def forward(self, steps: int) -> str:
while self.cur.next and steps > 0:
self.cur = self.cur.next
steps -= 1
return self.cur.valWhere is the number of visited urls, is the average length of each url, and is the number of steps we go forward or back.