How to Design a Browser History using Double-ended Queue (deque)
- 时间:2020-09-09 13:08:38
- 分类:网络文摘
- 阅读:118 次
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps.
Implement the BrowserHistory class:
BrowserHistory(string homepage) Initializes the object with the homepage of the browser.
void visit(string url) visits url from the current page. It clears up all the forward history.
string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps.
string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.Example:
Input:
1 2 3 4 ["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"]["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:
1 2 3 4 5 6 7 8 9 10 11 BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"Constraints:
1 <= homepage.length <= 20
1 <= url.length <= 20
1 <= steps <= 100
homepage and url consist of ‘.’ or lower case English letters.
At most 5000 calls will be made to visit, back, and forward.Hints:
Use two stack one for back history and one for forward history and simulate the functions.
Can you do faster by using different data structure?
Using deque in C++
The deque is a double-ended Queue in C++ meaning that you can push and pop in both directions.
Thus, we also need a cursor to store the current position in the queue, and update accordingly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | class BrowserHistory { public: BrowserHistory(string homepage) { history.push_back(homepage); cursor = 0; } void visit(string url) { while (history.size() - 1 > cursor) { history.pop_back(); } history.push_back(url); cursor ++; } string back(int steps) { if (steps > cursor) { cursor = 0; return history[0]; } cursor -= steps; return history[cursor]; } string forward(int steps) { if (steps + cursor >= history.size()) { cursor = history.size() - 1; return history[cursor]; } cursor += steps; return history[cursor]; } private: int cursor; deque<string> history; }; /** * Your BrowserHistory object will be instantiated and called as such: * BrowserHistory* obj = new BrowserHistory(homepage); * obj->visit(url); * string param_2 = obj->back(steps); * string param_3 = obj->forward(steps); */ |
class BrowserHistory {
public:
BrowserHistory(string homepage) {
history.push_back(homepage);
cursor = 0;
}
void visit(string url) {
while (history.size() - 1 > cursor) {
history.pop_back();
}
history.push_back(url);
cursor ++;
}
string back(int steps) {
if (steps > cursor) {
cursor = 0;
return history[0];
}
cursor -= steps;
return history[cursor];
}
string forward(int steps) {
if (steps + cursor >= history.size()) {
cursor = history.size() - 1;
return history[cursor];
}
cursor += steps;
return history[cursor];
}
private:
int cursor;
deque<string> history;
};
/**
* Your BrowserHistory object will be instantiated and called as such:
* BrowserHistory* obj = new BrowserHistory(homepage);
* obj->visit(url);
* string param_2 = obj->back(steps);
* string param_3 = obj->forward(steps);
*/As the visit rewrites the forward history, we need to pop back all the URLs from the current cursor+1 to the end of the queue before we push the new URL to the queue (rewrite the forward history). The time complexity is O(N) where N is the maximum number of the URLs in history.
The back and forward operations are trivial O(1) time complexity as we only need to check the boundaries and update the cursor accordingly.
Oh, just realised that in this particular problem, you don’t need deque, you can use just use a stack (as we are only push and pop from one-end only).
–EOF (The Ultimate Computing & Technology Blog) —
推荐阅读:诗词名句鉴赏:魂兮归来哀江南 诗词名句鉴赏:惟草木之零落兮,恐美人之迟暮。 诗词名句鉴赏:身既死兮神以灵,魂魄毅兮为鬼雄! 诗词名句鉴赏:嘤其鸣矣,求其有声。 数学题:化肥厂计划用15天生产化肥4500吨 数学题:学校把两捆树苗分给三个年级种植 数学题:甲乙丙三人的平均年龄为22岁 数学题:在一块边长60m的正方形花坛四边种冬青树 数学题:用一根铁丝刚好焊成一个棱长10厘米的正方体框架 数学题:一艘船在静水中每小时行驶40千米
- 评论列表
-
- 添加评论