• 算法05设计getMin功能的栈


    描述

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

    输入描述:

    第一行输入一个整数N,表示对栈进行的操作总数。

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

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

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

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

    输出描述:

    对于每个getMin操作,输出一行表示当前栈中的最小元素是多少。

    示例1

    输入:
    6
    push 3
    push 2
    push 1
    getMin
    pop
    getMin
    
    输出:
    1
    2

    思路

    使用俩个栈,一个栈用来保存当前栈中的元素,其功能和一个正常的栈没有区别,记为stackData;另外一个栈用于保存每一步的最小值,记为stackMin;
    (1) 对于压栈操作,正常栈直接压栈;而最小栈需要判断当前要压入的元素是否比栈顶小,如果小,直接压入,否则仍然压入此时的栈顶元素,以同步保证栈顶为截至到目前为止正常栈的最小值。
    (2) 弹栈只需要同时弹出两个栈的栈顶元素。
    (3) 获得最小值只需要返回最小栈的栈顶即可。
    具体实现有俩种方式:
     
    方法一:
    import java.util.Stack;
    import java.util.Scanner;
     
    class MyStack1{
        private Stack<Integer> stackData;
        private Stack<Integer> stackMin;
         
        public MyStack1(){
            this.stackData = new Stack<>();
            this.stackMin = new Stack<>();
        }
         
        public void push(int newNum){
            if(this.stackMin.isEmpty()){
                this.stackMin.push(newNum);
            } else if(newNum<=this.top()){
                this.stackMin.push(newNum);
            }
            this.stackData.push(newNum);
        }
         
        public int pop(){
            if(this.stackData.isEmpty()){
                throw new RuntimeException("Your stack is empty.");
            }
            int value = this.stackData.pop();
            if(value==this.top()){
                this.stackMin.pop();
            }
            return value;
        }
         
        public int top(){
            return this.stackMin.peek();
        }
         
        public int getMin(){
            if(this.stackMin.isEmpty()){
                throw new RuntimeException("Your stack is empty.");
            }
            return this.stackMin.peek();
        }
         
    }
     
    public class Main{
        public static void main(String[] args){
        Scanner sc= new Scanner(System.in);
        MyStack1 mystack1 = new MyStack1();
        int t = sc.nextInt();
             
        for(int i=0;i<t;i++){
            String op = sc.next();
            if(op.equals("push")){
                int x = sc.nextInt();
                mystack1.push(x);
            }else if(op.equals("getMin")){
                System.out.println(mystack1.getMin());
            }else if(op.equals("pop")){
                mystack1.pop();
            }
        }
      }
    }

    方法二:

    import java.util.Stack;
    import java.util.Scanner;
    
    class MyStack2{
        private Stack<Integer> stackData;
        private Stack<Integer> stackMin;
         
        public MyStack2(){
            this.stackData = new Stack<>();
            this.stackMin = new Stack<>();
        }
         
        public void push(int newNum){
            if(this.stackMin.isEmpty()){
                this.stackMin.push(newNum);
            } else if(newNum<=this.getMin()){
                this.stackMin.push(newNum);
            }else{
                int newMin = this.stackMin.peek();
                this.stackMin.push(newMin);
            }
            this.stackData.push(newNum);
        }
         
        public int pop(){
            if(this.stackData.isEmpty()){
                throw new RuntimeException("Your stack is empty.");
            }
            this.stackData.pop();
            return this.stackMin.pop();
        }
         
        public int getMin(){
            if(this.stackMin.isEmpty()){
                throw new RuntimeException("Your stack is empty.");
            }
            return this.stackMin.peek();
        }
         
    }
     
    public class Main{
        public static void main(String[] args){
        Scanner sc= new Scanner(System.in);
        MyStack2 mystack2 = new MyStack2();
        int t = sc.nextInt();
             
        for(int i=0;i<t;i++){
            String op = sc.next();
            if(op.equals("push")){
                int x = sc.nextInt();
                mystack2.push(x);
            }else if(op.equals("getMin")){
                System.out.println(mystack2.getMin());
            }else if(op.equals("pop")){
                mystack2.pop();
            }
        }
      }
    }
  • 相关阅读:
    UVA 10604 Chemical Reaction
    UVA 10635 Prince and Princess
    UVA 607 Scheduling Lectures
    Create Maximo Report
    安裝及配置Maximo Report步驟
    check blocking
    數據據類型縮寫
    .net
    poj3522
    poj1286
  • 原文地址:https://www.cnblogs.com/sfnz/p/15781994.html
Copyright © 2020-2023  润新知