上次编辑到这里,代码来自缓存 点击恢复默认模板
class BrowserHistory {
public:
BrowserHistory(string homepage) {
}
void visit(string url) {
}
string back(int steps) {
}
string forward(int steps) {
}
};
/**
* 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);
*/
golang 解法, 执行用时: 80 ms, 内存消耗: 7.5 MB, 提交时间: 2022-12-04 11:33:08
type BrowserHistory struct {
dummy, cur *MyLinkList
}
type MyLinkList struct{
Pre, Next *MyLinkList
Val string
}
func Constructor(homepage string) BrowserHistory {
cur := &MyLinkList{Val:homepage}
return BrowserHistory{dummy:cur, cur:cur}
}
func (this *BrowserHistory) Visit(url string) {
cur := &MyLinkList{Val:url}
this.cur.Next, cur.Pre = cur, this.cur
this.cur = cur
}
func (this *BrowserHistory) Back(steps int) string {
for i:=0; i<steps && this.cur!=this.dummy; i++ {
this.cur=this.cur.Pre
}
return this.cur.Val
}
func (this *BrowserHistory) Forward(steps int) string {
for i:=0; i<steps && this.cur.Next != nil; i++ {
this.cur=this.cur.Next
}
return this.cur.Val
}
/**
* Your BrowserHistory object will be instantiated and called as such:
* obj := Constructor(homepage);
* obj.Visit(url);
* param_2 := obj.Back(steps);
* param_3 := obj.Forward(steps);
*/
golang 解法, 执行用时: 84 ms, 内存消耗: 7.6 MB, 提交时间: 2022-12-04 11:32:08
// 浏览器返回功能,增加游标cur,back和forward时操作cur,
type BrowserHistory struct {
history []string
cur int // 当前栈顶index
}
func Constructor(homepage string) BrowserHistory {
return BrowserHistory{
history: []string{homepage},
cur: 0,
}
}
func (b *BrowserHistory) Visit(url string) {
b.history = b.history[:b.cur+1]
b.history = append(b.history, url)
b.cur++
}
func (b *BrowserHistory) Back(steps int) string {
if steps > b.cur {
steps = b.cur
}
b.cur -= steps
return b.history[b.cur]
}
func (b *BrowserHistory) Forward(steps int) string {
if steps > len(b.history)-b.cur-1 {
steps = len(b.history) - b.cur-1
}
b.cur += steps
return b.history[b.cur]
}
/**
* Your BrowserHistory object will be instantiated and called as such:
* obj := Constructor(homepage);
* obj.Visit(url);
* param_2 := obj.Back(steps);
* param_3 := obj.Forward(steps);
*/
python3 解法, 执行用时: 152 ms, 内存消耗: 17.5 MB, 提交时间: 2022-12-04 11:31:18
class BrowserHistory:
def __init__(self, homepage: str):
self.a = [homepage]
self.i = 0 #当前的下标(index)
def visit(self, url: str) -> None:
del self.a[self.i + 1 : ]
self.a.append(url)
self.i += 1
def back(self, steps: int) -> str:
self.i = max(0, self.i - steps)
return self.a[self.i]
def forward(self, steps: int) -> str:
self.i = min(self.i + steps, len(self.a) - 1)
return self.a[self.i]
# 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)
cpp 解法, 执行用时: 116 ms, 内存消耗: 68.1 MB, 提交时间: 2022-12-04 11:29:56
class BrowserHistory {
public:
int pos;
int top;
string history[5001];
BrowserHistory(string homepage) : pos(-1), top(0) {
visit(homepage);
}
void visit(string url) {
pos ++;
top = pos;
history[top++] = url;
}
string back(int steps) {
if(steps > pos) {
steps = pos;
}
pos -= steps;
return history[pos];
}
string forward(int steps) {
steps = min(steps, top - pos - 1);
pos += steps;
return history[pos];
}
};
/**
* 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);
*/