-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1472-Design_Browser_History.cpp
More file actions
85 lines (78 loc) · 2.73 KB
/
1472-Design_Browser_History.cpp
File metadata and controls
85 lines (78 loc) · 2.73 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*******************************************************************************
* 1472-Design_Browser_History.cpp
* Billy.Ljm
* 18 Mar 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/design-browser-history/
* 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.
*
* ===========
* My Approach
* ===========
* The questions is just asking us to remember the past few strings, and
* navigate between them. These strings can be stored as a vector and we just
* have to move the index around.
******************************************************************************/
#include <iostream>
#include <string>
#include <vector>
class BrowserHistory {
public:
std::vector<std::string> history; // history of pages
size_t index; // current index of `history`
BrowserHistory(std::string homepage) {
//history.clear();
history.push_back(homepage);
index = 0;
}
void visit(std::string url) {
history.resize(index + 1); // clear forward history
history.push_back(url);
index++;
}
std::string back(int steps) {
if (steps > index) {
index = 0;
}
else {
index -= steps;
}
return history[index];
}
std::string forward(int steps) {
index += steps;
if (index >= history.size()) {
index = history.size() - 1;
}
return history[index];
}
};
int main(void) {
BrowserHistory browserHistory = BrowserHistory("leetcode.com");
browserHistory.visit("google.com");
browserHistory.visit("facebook.com");
browserHistory.visit("youtube.com");
std::cout << browserHistory.back(1) << std::endl; // return "facebook.com"
std::cout << browserHistory.back(1) << std::endl; // return "google.com"
std::cout << browserHistory.forward(1) << std::endl; // return "facebook.com"
browserHistory.visit("linkedin.com");
browserHistory.forward(2);
std::cout << browserHistory.back(2) << std::endl; // return "google.com"
std::cout << browserHistory.back(7) << std::endl; // return "leetcode.com"
}