T1:leetcode.1472 设计浏览器历史记录
class BrowserHistory {
public:
BrowserHistory(string homepage) {
urls.resize(5000);
urls[0] = homepage;
current = 0;
size = 1;
}
void visit(string url) {
urls[++current] = url;
size = current + 1;
}
string back(int steps) {
current = current > steps ? current - steps : 0;
return urls[current];
}
string forward(int steps) {
current = (current + steps) >= size ? size - 1 : (current + steps);
return urls[current];
}
private:
vector<string> urls;
int current;
int size;
};
/**
* 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);
*/
/*
1.current 用来记录当前网页位置
size 用来记录所有的网页的个数
2.回退超过current 则urls[0]
前进超过size 则urls[size - 1]
3.注意小的边界条件 未能通过全部案例 边界条件考虑不完善
*/