11/9 <Stack> 155 232 225

155. Min Stack

class MinStack {
    int min = Integer.MAX_VALUE;
    Stack<Integer> stack = new Stack<Integer>();
    /** initialize your data structure here. */
    public MinStack() {

    }

    public void push(int x) {
        if(x <= min){
            stack.push(min);
            min = x;
        }
        stack.push(x);
    }

    public void pop() {
        if(stack.pop() == min) min = stack.pop();
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return min;
    }
}

232. Implement Queue using Stacks

用一个input和outlput栈,当pop时,output为空,则把input全部压入output中。

class MyQueue {
    Stack<Integer> input = new Stack();
    Stack<Integer> output = new Stack();
    /** Initialize your data structure here. */
    public MyQueue() {

    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        input.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        peek();
        return output.pop();
    }

    /** Get the front element. */
    public int peek() {
        if(output.empty())
            while(!input.empty())
                output.push(input.pop());
        return output.peek();
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return input.empty() && output.empty();
    }
}

225. Implement Stack using Queues

在push的时候就直接把顺序缓过来

class MyStack {
    Queue<Integer> queue;
    /** Initialize your data structure here. */
    public MyStack() {
        this.queue = new LinkedList<Integer>();
    }

    /** Push element x onto stack. */
    public void push(int x) {
        queue.add(x);
        for(int i = 0; i < queue.size() - 1; i++){
            queue.add(queue.poll());
        }
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return queue.poll();
    }

    /** Get the top element. */
    public int top() {
        return queue.peek();
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue.isEmpty();
    }
}

原文地址:https://www.cnblogs.com/Afei-1123/p/11827334.html

时间: 2024-11-13 08:53:32

11/9 <Stack> 155 232 225的相关文章

D3_book 11.2 stack

<!-- book :interactive data visualization for the web 11.2 stack 一个堆叠图的例子 --> <!DOCTYPE html> <meta charset="utf-8"> <style> </style> <body> <script src="../d3.min.js"></script> <scrip

每日一题之LeetCode 栈简单题集合496,682,232,225,155,844,20

496 下一个最大的元素方法1 :自己瞎写的 没有考虑到栈的特性class Solution:def nextGreaterElement(self, nums1, nums2): L=[] for i in nums1: k=nums2.index(i) lenth2=len(nums2) if k==lenth2-1: L.append(-1) for j in range(k+1,lenth2): if nums2[j]>i: L.append(nums2[j]) break if j==

11月26号host

127.0.0.1 localhost255.255.255.255 broadcasthost::1 localhostfe80::1%lo0 localhost # Google start216.239.38.125 gmail-imap.l.google.com216.239.38.125 googlemail-imap.l.google.com216.239.38.125 gmail-smtp.l.google.com216.239.38.125 googlemail-smtp.l.g

Google Hosts (2015.11.03)

## # Copyright (c) 2014-2015, racaljk. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY. Redistribution and use in anyway, DO # NOT remove these and follow comments. # # Host Database # # localhost is use

第11章 认识和学习bash

认识bash这个shell 硬件.内核和shell 用户操作计算机流程如下: 用户——>用户界面(shell,KDE,application)——>核心(kernel)——>硬件(hardware) 应用程序处于最外层,因此称为shell,shell的功能是提供用户操作系统的一个接口.狭义的shell指的是命令行软件(包括bash),广义的shell包括图形界面软件 系统的合法shell和/etc/shells功能 系统合法的shell都写入到/etc/shells这个软件,查看该文件如

11月16host文件

############################################################################################################################################## 127.0.0.1 localhost255.255.255.255 broadcasthost::1 localhostfe80::1%lo0 localhost # Google start64.233.188

UI设计师零基础入门到精通精品视频教程【155课高清完整版】

[福吧资源网分享]课程是非常完整的,也是非常零基础的,适合任何学员,有需要的可以下载看看!课程目录:第1章 Adobe Photoshop CS6课时1 Adobe Photoshop CS6入门基础 52:24课时2 创建与编辑选区(上) 46:57课时3 创建与编辑选区(下) 48:59课时4 掌握图层与图层组(上) 48:01课时5 掌握图层与图层组(下) 57:35课时6 绘制位图图像(上) 47:46课时7 绘制位图图像(下) 46:37课时8 绘制矢量图形(上) 46:49课时9 绘

Coursera Algorithms week2 栈和队列 练习测验: Stack with max

题目原文: Stack with max. Create a data structure that efficiently supports the stack operations (push and pop) and also a return-the-maximum operation. Assume the elements are reals numbers so that you can compare them. 分析: 该题目要求在实现正常stack的push和pop操作外,还

Stack栈——getMin()

获取栈--stack中最小的元素 建立另一个栈--temp 如果两个栈都为空,则将node同时push两个栈中 接着插入new_node,如果temp不为空,则比较栈顶node->data和new_node->data. 如果node->data大于new_node->data,则将new_node同时push两个栈中 反之,只push进stack栈中 1 #include<stdio.h> 2 #include<stdlib.h> 3 4 struct s