首页 > 其他分享 >【栈】LeetCode 1472. 设计浏览器历史记录

【栈】LeetCode 1472. 设计浏览器历史记录

时间:2023-01-05 10:35:45浏览次数:58  
标签:浏览器 String temp public 1472 steps push LeetCode history

题目链接

1472. 设计浏览器历史记录

思路

用栈 history 模拟网页的前进后退操作,用栈 temp 来暂时存储后退所退出的网页。

代码

class BrowserHistory{
    Stack<String> history;
    Stack<String> temp;

    public BrowserHistory(String homepage){

        this.history = new Stack<>();
        this.history.push(homepage);
        this.temp = new Stack<>();
    }

    public void visit(String url){

        this.history.push(url);
        this.temp.clear();
    }

    public String back(int steps){

        while(steps > 0 && this.history.size() > 1){
            this.temp.push(this.history.pop());
            steps--;
        }

        return this.history.peek();
    }

    public String forward(int steps){

        while(steps > 0 && this.temp.size() > 0){
            this.history.push(this.temp.pop());
            steps--;
        }

        return this.history.peek();
    }
}

标签:浏览器,String,temp,public,1472,steps,push,LeetCode,history
From: https://www.cnblogs.com/shixuanliu/p/17026835.html

相关文章

  • [LeetCode] 2453. Destroy Sequential Targets
    Youaregivena 0-indexed array nums consistingofpositiveintegers,representingtargetsonanumberline.Youarealsogivenaninteger space.Youhave......
  • 监听浏览器资源载入完成和节点载入完成
    浏览器控制台“网络”选项卡这里可以看到浏览器的资源载入和节点载入完成时间和情况。有时候,我们需要在浏览器节点载入完成之后做什么、浏览器资源载入完成之后做什么。......
  • leetcode-653. 两数之和 IV - 输入二叉搜索树
    653.两数之和IV-输入二叉搜索树-力扣(Leetcode)用了迭代进行遍历二叉树,因为二叉搜索树的中序遍历是有序的,所以肯定左边大于右边,然后就可以用一个map来存放之前的数值,......
  • [LeetCode]015-三数之和
    >>>传送门题目给你一个整数数组nums,判断是否存在三元组[nums[i],nums[j],nums[k]]满足i!=j、i!=k且j!=k,同时还满足nums[i]+nums[j]+nums[k]==0......
  • leetcode-387-easy
    FirstUniqueCharacterinaStringGivenastrings,findthefirstnon-repeatingcharacterinitandreturnitsindex.Ifitdoesnotexist,return-1.Examp......
  • leetcode-414-easy
    ThirdMaximumNumberGivenanintegerarraynums,returnthethirddistinctmaximumnumberinthisarray.Ifthethirdmaximumdoesnotexist,returnthemaxim......
  • leetcode-563-easy
    BinaryTreeTiltGiventherootofabinarytree,returnthesumofeverytreenode'stilt.Thetiltofatreenodeistheabsolutedifferencebetweenthesum......
  • leetcode-349-easy
    IntersectionofTwoArraysGiventwointegerarraysnums1andnums2,returnanarrayoftheirintersection.Eachelementintheresultmustbeuniqueandyoum......
  • leetcode-350-easy
    IntersectionofTwoArraysIIGiventwointegerarraysnums1andnums2,returnanarrayoftheirintersection.Eachelementintheresultmustappearasmany......
  • leetcode-367-easy
    ValidPerfectSquareGivenapositiveintegernum,returntrueifnumisaperfectsquareorfalseotherwise.Aperfectsquareisanintegerthatisthesquar......