Leetcode 1472. Design browser history
You have a browser that only supports a single tab. The first page you browse is homepage. You can visit other website URLs, or step back or step forward in browsing history.
Please implement BrowserHistory class:
- BrowserHistory(string homepage), initialize the browser class with homepage.
- void visit(string url) jumps from the current page to visit the page corresponding to the url. Performing this operation will delete all records of browsing history advance.
- string back(int steps) step back in browsing history. If you can only go back up to x steps in browsing history and steps > x, then you only go back x steps. Please go back to the url after the most steps step.
- string forward(int steps) steps forward in browsing history. If you can only advance up to x steps in browsing history and steps > x, then you only advance x steps. Please return to the url after the most steps.
Example:
Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] //Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] //Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You were browsing“ leetcode.com " . Visit“ google.com " browserHistory.visit("facebook.com"); // You were browsing“ google.com " . Visit“ facebook.com " browserHistory.visit("youtube.com"); // You were browsing“ facebook.com " . Visit“ youtube.com " browserHistory.back(1); // You were browsing“ youtube.com ", back to" facebook.com "And back" facebook.com " browserHistory.back(1); // You were browsing“ facebook.com ", back to" google.com "And back" google.com " browserHistory.forward(1); // You were browsing“ google.com ", forward to" facebook.com "And back" facebook.com " browserHistory.visit("linkedin.com"); // You were browsing“ facebook.com " . Visit“ linkedin.com " browserHistory.forward(2); // You were browsing“ linkedin.com ", you can't go any further. browserHistory.back(2); // You were browsing“ linkedin.com "First in two steps" facebook.com ", and then to" google.com ", and return" google.com " browserHistory.back(7); // You were browsing“ google.com ", you can only step back to" leetcode.com ", and return" leetcode.com "
Tips:
- 1 <= homepage.length <= 20
- 1 <= url.length <= 20
- 1 <= steps <= 100
- Both homepage and url contain only '.' or lowercase letters.
- Call up to 5000 visit, back, and forward functions.
Method: double linked list
Ideas:
This problem is of great engineering significance. The key is what data structure to use. You can use stack plus two pointers. I use bidirectional linked list.
Maintain two linked list nodes in the class, a header node, to store the homepage, and a now node to store the currently accessed node.
visit method: connect a new node behind the current node, and then update the now node to point to the new node. (this means that the nodes behind the original now have been abandoned, which is consistent with the topic).
back method: find the previous node through the. pre of the now node. If it is already the head node, return to homepage, update the now node continuously, and find the website before steps.
forward method: similar to the back above, advance several steps. If you reach the last node, break and return to the last URL. Otherwise, continue. next backward.
code:
class Listnode: #Define double linked list node def __init__(self, x): self.val = x self.pre = None self.next = None class BrowserHistory: def __init__(self, homepage: str): #Initialization, head node val is homepage, current node self.now As head node self.head = Listnode(homepage) self.now = self.head def visit(self, url: str) -> None: #visit will delete all subsequent records, so you can directly create a new linked list node and add it to the current node temp = Listnode(url) #After connecting to the current node self.now.next = temp temp.pre = self.now #Update current node self.now = temp def back(self, steps: int) -> str: #If it is already a header node and cannot back, return home page if self.now == self.head: return self.head.val #Start steps back for _ in range(steps): if self.now == self.head: break self.now = self.now.pre return self.now.val def forward(self, steps: int) -> str: #Step forward for _ in range(steps): #If you get to the last one, you can't move forward. Go back if self.now.next == None: return self.now.val self.now = self.now.next return self.now.val # 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)