1472. Design Browser History
package LeetCode_1472 import java.util.* /** * 1472. Design Browser History * https://leetcode.com/problems/design-browser-history/description/ * * 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: 1. BrowserHistory(string homepage) Initializes the object with the homepage of the browser. 2. void visit(string url) Visits url from the current page. It clears up all the forward history. 3. 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. 4. 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. * */ class BrowserHistory(homepage: String) { /* * solution: two stack to save back and forward, and keep currentUrl * */ val stackBack = Stack<String>() val stackForward = Stack<String>() var currentUrl = "" init { currentUrl = homepage } fun visit(url: String) { //clears up all the forward history stackForward.clear() stackBack.add(currentUrl) currentUrl = url } fun back(steps: Int): String { var steps_ = steps while (steps_ > 0 && stackBack.isNotEmpty()){ //add current to forward stackForward.add(currentUrl) currentUrl = stackBack.pop() steps_-- } return currentUrl } fun forward(steps: Int): String { var steps_ = steps while (steps_ > 0 && stackForward.isNotEmpty()){ //add current to back stackBack.add(currentUrl) currentUrl = stackForward.pop() steps_-- } return currentUrl } } /** * Your BrowserHistory object will be instantiated and called as such: * var obj = BrowserHistory(homepage) * obj.visit(url) * var param_2 = obj.back(steps) * var param_3 = obj.forward(steps) */
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)