前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >设计getMin功能的栈 顶

设计getMin功能的栈 顶

作者头像
须臾之余
发布2020-03-19 10:03:58
2610
发布2020-03-19 10:03:58
举报
文章被收录于专栏:须臾之余须臾之余

题目描述

实现一个特殊功能的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作。

输入描述:

代码语言:javascript
复制
第一行输入一个整数N,表示对栈进行的操作总数。

下面N行每行输入一个字符串S,表示操作的种类。

如果S为"push",则后面还有一个整数X表示向栈里压入整数X。

如果S为"pop",则表示弹出栈顶操作。

如果S为"getMin",则表示询问当前栈中的最小元素是多少。

输出描述:

代码语言:javascript
复制
对于每个getMin操作,输出一行表示当前栈中的最小元素是多少。

示例1

输入

复制

代码语言:javascript
复制
6
push 3
push 2
push 1
getMin
pop
getMin

输出

复制

代码语言:javascript
复制
1
2

备注:

代码语言:javascript
复制
1<=N<=1000000

-1000000<=X<=1000000

数据保证没有不合法的操作

第一种设计方案:

代码语言:javascript
复制
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;

public class Main {
    static LinkedList<Node> stack=new LinkedList<Node>();
    static Head head=new Head();

    public static void push(int val){
        Node node =new Node(val);
        stack.addFirst(node);
        //第一个元素
        if (head.first == null){
            head.first = node;
            head.last=node;
            return;
        }
        //插入到头
        if (head.first.val >= val) {
            node.after=head.first;
            head.first.before=node;
            head.first=node;
            return;
        }
        //插入到尾部
        if (head.last.val <= val){
            head.last.after=node;
            node.before=head.last;
            head.last=node;
            return;
        }
        //在中间插入
        Node next=head.first;
        while (next!=null){
            if (next.val >= val){
                node.after=next;
                node.before=next.before;
                next.before.after=node;
                next.before=node;
                return;
            }
            next=next.after;
        }
    }
    public static void pop(){
        Node node = stack.removeFirst();
        //移除前面那个元素
       if (node.before == null){
           if (node.after!=null){
               node.after.before=null;
           }
           head.first=node.after;
       }
       if (node.after == null){
           if (node.before !=null){
               node.before.after=null;
           }
           head.last=node.before;
       }
       if (node.after!=null && node.before!=null){
           node .before.after=node.after;
           node.after.before=node.before;
       }
    }
    public static void getMin(){
        System.out.println(head.first.val);
    }

    public static void main(String[] args) throws IOException {
        BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
        int row = Integer.valueOf(scanner.readLine());
        for (int i = 0; i < row; i++) {
            String[] data = scanner.readLine().split(" ");
            if(data[0].equals("push")) {
                push(Integer.valueOf(data[1]));
                continue;
            }
            if (data[0].equals("pop")) {
                pop();
                continue;
            }
            getMin();
        }
        scanner.close();
    }
}
class Head{
    Node first;
    Node last;
}
class Node{
    Node before;
    int val;
    Node after;

    public Node(int val) {
        this.val = val;
    }
}

简单说明下:

push操作

pop操作也类似,这里就不说了。

运行结果

第二种设计方案:

代码语言:javascript
复制
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

/**
 * 题目描述
 * 实现一个特殊功能的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作。
 */
public class MyStack1 {

    private Stack<Integer>stackData;
    private Stack<Integer>stackMin;

    public MyStack1() {
        this.stackData = new Stack<>();
        this.stackMin = new Stack<>();
    }
    public void push(int nuwNum){
        this.stackData.push(nuwNum);
        if (this.stackMin.isEmpty() || nuwNum <= this.stackMin.peek()){
            this.stackMin.push(nuwNum);
        }else {
            int newMin=this.stackMin.peek();
            this.stackMin.push(newMin);
        }
    }
    public void pop(){
        if (this.stackData.isEmpty()){
            throw new RuntimeException("your stack is empty!");
        }
        this.stackMin.pop();
        this.stackData.pop();

    }
    public void getMin(){
        if (this.stackMin.isEmpty()){
            throw new RuntimeException("your stack is empty!");
        }
        System.out.println(this.stackMin.peek());
    }

    public static void main(String[] args) throws IOException {
        BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
        int row = Integer.valueOf(scanner.readLine());
        MyStack1 myStack1 = new MyStack1();
        for (int i = 0; i < row; i++) {
            String[] data = scanner.readLine().split(" ");
            if(data[0].equals("push")) {
                myStack1.push(Integer.valueOf(data[1]));
                continue;
            }
            if (data[0].equals("pop")) {
                myStack1.pop();
                continue;
            }
            myStack1.getMin();
        }
        scanner.close();
    }

}

结果:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 输入描述:
  • 输出描述:
  • 输入
  • 输出
  • 备注:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档