列表

详情


1472. 设计浏览器历史记录

你有一个只支持单个标签页的 浏览器 ,最开始你浏览的网页是 homepage ,你可以访问其他的网站 url ,也可以在浏览历史中后退 steps 步或前进 steps 步。

请你实现 BrowserHistory 类:

 

示例:

输入:
["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]]
输出:
[null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"]

解释:
BrowserHistory browserHistory = new BrowserHistory("leetcode.com");
browserHistory.visit("google.com");       // 你原本在浏览 "leetcode.com" 。访问 "google.com"
browserHistory.visit("facebook.com");     // 你原本在浏览 "google.com" 。访问 "facebook.com"
browserHistory.visit("youtube.com");      // 你原本在浏览 "facebook.com" 。访问 "youtube.com"
browserHistory.back(1);                   // 你原本在浏览 "youtube.com" ,后退到 "facebook.com" 并返回 "facebook.com"
browserHistory.back(1);                   // 你原本在浏览 "facebook.com" ,后退到 "google.com" 并返回 "google.com"
browserHistory.forward(1);                // 你原本在浏览 "google.com" ,前进到 "facebook.com" 并返回 "facebook.com"
browserHistory.visit("linkedin.com");     // 你原本在浏览 "facebook.com" 。 访问 "linkedin.com"
browserHistory.forward(2);                // 你原本在浏览 "linkedin.com" ,你无法前进任何步数。
browserHistory.back(2);                   // 你原本在浏览 "linkedin.com" ,后退两步依次先到 "facebook.com" ,然后到 "google.com" ,并返回 "google.com"
browserHistory.back(7);                   // 你原本在浏览 "google.com", 你只能后退一步到 "leetcode.com" ,并返回 "leetcode.com"

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
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);
 */

上一题