首页 > 其他分享 >[代码随想录] 第九天

[代码随想录] 第九天

时间:2024-01-19 17:48:00浏览次数:28  
标签:第九天 temp int 代码 随想录 pop public stackOut stackIn

232.栈实现队列[https://leetcode.cn/problems/implement-queue-using-stacks/description/]
思路:无

class MyQueue {
    Stack<Integer> stackIn;
    Stack<Integer> stackOut;
    int temp;

    public MyQueue() {
        stackIn = new Stack<>();
        stackOut = new Stack<>();
    }

    public void push(int x) {
        stackIn.push(x);
    }

    public int pop() {
        while (!stackIn.empty()) {
            temp = stackIn.pop();
            stackOut.push(temp);
        }
        int ans = stackOut.pop();
        while (!stackOut.empty()) {
            temp = stackOut.pop();
            stackIn.push(temp);
        }
        return ans;
    }

    public int peek() {
        while (!stackIn.empty()) {
            temp = stackIn.pop();
            stackOut.push(temp);
        }
        int ans = stackOut.peek();
        while (!stackOut.empty()) {
            temp = stackOut.pop();
            stackIn.push(temp);
        }
        return ans;
    }

    public boolean empty() {
        return stackIn.empty();
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */
**-----------------------分割线-------------------------**

225.用队列实现栈[https://leetcode.cn/problems/implement-stack-using-queues/submissions/496722251/]
思路:优化只使用了一条队列。

class MyStack {
    Queue<Integer> queue;
    int temp;

    public MyStack() {
        queue = new LinkedList<>();
    }

    public void push(int x) {
        queue.offer(x);
    }

    public int pop() {
        int i = 1;
        while (i < queue.size()) {
            temp = queue.poll();
            queue.offer(temp);
            i++;
        }
        return queue.poll();
    }

    public int top() {
        int i = 1;
        while (i <= queue.size()) {
            temp = queue.poll();
            queue.offer(temp);
            i++;
        }
        return temp;
    }

    public boolean empty() {
        return queue.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */

标签:第九天,temp,int,代码,随想录,pop,public,stackOut,stackIn
From: https://www.cnblogs.com/cssg/p/17975211

相关文章

  • [代码随想录] 第八天
    28.找出字符串中第一个匹配项的下标[https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/description/]思路:KMP算法,重点在于求NEXT数组。还不能理解..暂时先背下来了。classSolution{publicintstrStr(Stringhaystack,Stringneedle......
  • 使用 Swift 代码优化项目编译速度
    引言软件的性能是评价一个软件质量的重要指标,尤其在今天这个时代,性能已成为大型项目不可或缺的考虑因素之一。对于用户量极大的软件,如网银系统、在线购物商城等,更是必须保证其高效稳定的性能。在这种背景下,优化项目的编译速度就显得尤为重要。本文将介绍如何使用Swift代码优化......
  • file_put_contents之PHP伪协议绕过死亡代码
    前言一个CTF比赛中差不多用烂的一个考点,考察选手对php伪协议的灵活运用前置知识0x1Base64解码只解码字符表中的字符,对不在字符表中的字符会直接忽略(相当于置换为空),这里举个例子F12的base64编码是RjEy,我们插入一些不在字符表中的字符,仍然可以解码出正确结果0x2Base64编码......
  • 低代码为何这么“香”
    本文分享自华为云社区《【云享问答】第4期:低代码为何这么“香”》,作者:华为云社区精选。在数字化转型的大潮中,快速实现业务需求并迅速推出应用程序是企业成功的关键。而低代码平台,则成为了开发者的好帮手,为他们提供了高效、可扩展且可靠的开发解决方案。本期【云享问答】通过这11......
  • 把Mybatis Generator生成的代码加上想要的注释
    1前言在日常开发工作中,我们经常用MybatisGenerator根据表结构生成对应的实体类和Mapper文件。但是MybatisGenerator默认生成的代码中,注释并不是我们想要的,所以一般在Generator配置文件中,会设置不自动生成注释。带来的问题就是自动生成代码之后,我们还要自己去类文件中把注释加......
  • gitlab-runner实现gitlab上的代码自动打包发布——windows版
    gitlab中的项目,每次打包发布都比较麻烦,需要自己本地打包然后传输到服务器,再启动。考虑实现一种自动打包发布的方法。1)考虑使用jenkins实现,但是需要jdk11(当前环境不能升级),还有一些插件(这个虽然可以离线安装,但是需要高版本的jenkins,相对应的又依赖高版本的jdk)2)考......
  • 5分钟教会你如何在生产环境debug代码
    前言有时出现的线上bug在测试环境死活都不能复现,靠review代码猜测bug出现的原因,然后盲改代码直接在线上测试明显不靠谱。这时我们就需要在生产环境中debug代码,快速找到bug的原因,然后将锅丢出去。生产环境的代码一般都是关闭sourcemap和经过混淆的,那么如何进行debug代码呢?我一......
  • 《模拟龙生》|500行Go代码写一个随机冒险游戏|巨龙修为挑战开启
    一、前言新年就要到了,祝大家新的一年:......
  • 【教程】React-Native代码规范与加固详解
    引言ReactNative是一种跨平台的移动应用开发框架,由Facebook推出。它可以让我们使用JavaScript和React语法编写原生应用,大大提高了移动应用的开发效率。但是,对于开发人员来说,代码规范和安全性也是非常重要的问题。本篇博客将为大家详细介绍ReactNative的代码规范和加固......
  • 代码随想录 day23 修剪二叉搜索树 将有序数组转换为二叉搜索树 把二叉搜索树转换为累
    修剪二叉搜索树这道题不能直接写删除代码因为要涉及父子关系的保留如这样是暴力删掉不符合区间的节点但是没有保留父子关系这里我们把不符合区间的节点通过一个临时节点传递出来然后在外面合适方向接住具体怎么接住的呢其实就是对于root来说左边子树抛出的节点就会......