算法笔记001——栈

这篇博客探讨了如何使用Java实现特殊功能的栈,包括一个支持快速获取最小元素的栈和一个利用两个栈模拟队列操作的类。此外,还提出了一种仅使用递归函数实现栈内元素逆序的方法。这些实现都确保了关键操作的时间复杂度为O(1)。
摘要由CSDN通过智能技术生成

参考书本《程序员代码面试指南》

import java.util.Stack;
/**设计一个getMin功能的栈
【题目】实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作。
【要求】  1.pop、push、getMin操作的时间复杂度都是O(1)
        2.设计的栈类型可以使用现成的栈结构。
【难度】*
 **/
public class Mystack1 {

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

    public Mystack1(){
        this.stackData = new Stack<Integer>();
        this.stackMin = new Stack<Integer>();
    }
    //进栈操作,进行判断+对比:为空报错,不为空如果小于最小值的话还需要加紧stackMin中
    public void push(int newNum){
        if (this.stackData.isEmpty()){
            this.stackMin.push(newNum);

        }else if(newNum <= this.getmin()){
            this.stackMin.push(newNum);
        }
        this.stackData.push(newNum);
    }
    //定义弹出操作,如果弹出值是最小值的话还需要将最小值栈给弹出
    public int pop(){
        if (this.stackData.isEmpty()){
            throw new RuntimeException("stack is empty.");
        }
        int value = this.stackData.pop();
        if (value == this.getmin()){
            this.stackMin.pop();
        }
        return  value;

    }
    //定义getmin函数获取最小值,时间复杂度为O(1)
    private int getmin() {
        if(this.stackMin.isEmpty()){
            throw new RuntimeException("Your stack is empty.");
        }
        return this.stackMin.peek();
    }


}

解法2:

import java.util.Stack;

public class Mystack2 {
    private Stack<Integer> stackData;
    private Stack<Integer> stackMin;

    public Mystack2(){
        this.stackData = new Stack<Integer>();
        this.stackMin = new Stack<Integer>();
    }

    public void push(int newNum){
        if(stackMin.isEmpty()){
            this.stackMin.push(newNum);
        }else if(newNum < stackMin.peek()){
            this.stackMin.push(newNum);
        }else{
            int min = this.stackMin.peek();
            this.stackMin.push(min);
        }
        this.stackData.push(newNum);
    }

    public int pop(){
        if(this.stackData.isEmpty()){
            throw new RuntimeException("your stack is empty!");
        }else{
            this.stackMin.pop();
            return this.stackData.pop();
        }
    }

    public int getMin(){
        if (this.stackData.isEmpty()){
            throw new RuntimeException("this stack is empty!");
        }
        return this.stackMin.peek();
    }
}

题目3:

package 第一章;

import java.util.Stack;

/**
 * 【题目】
 * 编写一个类,用两个栈实现队列的操作,支持队列的基本操作(add添加,poll头部弹出(且删除),peek查看首元素)
 * 【难度】**
 * 【解题】
 * 定义两个栈,stackPush和stackPop,一个负责进队列,一个负责出队列,。
 *
 */
public class TwoStackQueue {
    public Stack<Integer> stackPush;
    public Stack<Integer> stackPop;

    public TwoStackQueue(){
        stackPush = new Stack<Integer>();
        stackPop = new Stack<Integer>();
    }

    //push栈向pop栈倒入数据
    private void pushToPop(){
        if (stackPop.isEmpty()){
            while (!stackPush.isEmpty()){
                stackPop.push(stackPush.pop());
            }
        }
    }

    //add操作,将数据存入stackPush栈
    public void add(int newNum){
        stackPush.push(newNum);
        pushToPop();
    }

    //poll操作,将元素从pop栈中弹出并删除
    public int poll(){
        if (stackPop.isEmpty() && stackPush.isEmpty()){
            throw new RuntimeException("stack is empty!");
        }
        pushToPop();
        return stackPop.pop();
    }

    //peek查看队列头元素
    public int peek(){
        if (stackPop.isEmpty() && stackPush.isEmpty()){
            throw new RuntimeException("stack is empty!");
        }
        pushToPop();
        return stackPop.peek();
    }
}

/**
 测试类
 **/
class TwoStackQueueTest{
    public static void main(String[] args) {
        TwoStackQueue twoStackQueue = new TwoStackQueue();
        //add方法
        twoStackQueue.add(1);
        twoStackQueue.add(2);
        twoStackQueue.add(3);
        twoStackQueue.add(4);
        twoStackQueue.add(5);

        //poll方法
        int poll = twoStackQueue.poll();
        System.out.println(poll);

        //peek查看元素
        int peek = twoStackQueue.peek();
        System.out.println(peek);
    }
}

题目4:

package 第一章;

import java.util.Stack;

/**
 * 【题目】
 * 一个栈依次压入1、2、3、4、5,那么从栈顶到栈底分别为5、4、3、2、1.
 * 将这个栈转职,从栈顶到栈底分别为1、2、3、4、5,也就是实现栈中元素的逆序,
 * 但智能用递归函数实现,不能用其他数据结构
 * 【难度】**
 * 【思路】
 * 通过两个递归甘薯解决
 */
public class getAndRemovelastElement3 {
    //递归函数1:将栈stack的栈底元素返回并移除
    public int getAndRemoveLastElement(Stack<Integer> stack){
        int result = stack.pop();
        if (stack.isEmpty()){
            return result;
        }else{
            int last = getAndRemoveLastElement(stack);
            stack.push(result);
            return last;
        }
    }

    //递归函数2:逆序一个栈,结合上面的递归函数1实现
    public void reverse(Stack<Integer> stack){
        if (stack.isEmpty()){
            return;
        }
        int i = getAndRemoveLastElement(stack);
        reverse(stack);
        stack.push(i);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值