美文网首页
栈| Leetcode 1472

栈| Leetcode 1472

作者: 三更冷 | 来源:发表于2023-03-27 16:37 被阅读0次

Leetcode 分类刷题 —— 栈(Stack)

1、题目描述:

Leetcode 1472. Design Browser History 设计浏览器历史记录

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

输入:
["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"

过程如下:
leetcode.com(当前)
leetcode.com -> google.com(当前)
leetcode.com -> google.com -> facebook.com(当前)
leetcode.com -> google.com -> facebook.com -> youtube.com(当前)
leetcode.com -> google.com -> facebook.com(当前)
leetcode.com -> google.com(当前)
leetcode.com -> google.com -> facebook.com(当前)
leetcode.com -> google.com -> facebook.com -> linkedin.com(当前)
leetcode.com -> google.com(当前)
leetcode.com(当前)

2、解题思路:

访问新的url,push后退历史,clear前进历史;
后退steps步,push前进历史,pop后退历史;
前进steps步,push后退历史,pop前进历史;

3、代码

import java.util.*;

public class BrowserHistory {
    private String homepage;
    private Deque<String> history;
    private Deque<String> forward;

    public BrowserHistory(String homepage) {
        this.homepage = homepage;
        this.history = new ArrayDeque<>();
        this.forward = new ArrayDeque<>();
        history.push(homepage);
    }

    public void visit(String url) {
        history.push(url);
        // clear forward history when visiting a new page
        forward.clear();
    }

    public String back(int steps) {
        // limit steps to the size of the history
        steps = Math.min(steps, history.size() - 1);
        // move steps back in history
        while (steps-- > 0) {
            forward.push(history.pop());
        }
        // return the current url after moving back
        return history.peek();
    }

    public String forward(int steps) {
        // limit steps to the size of the forward history
        steps = Math.min(steps, forward.size());
        // move steps forward in the forward history
        while (steps-- > 0) {
            history.push(forward.pop());
        }
        // return the current url after moving forward
        return history.peek();
    }
}

相关文章

网友评论

      本文标题:栈| Leetcode 1472

      本文链接:https://www.haomeiwen.com/subject/qylpldtx.html