从一个增强型的栈上 获得的启示

    • 前言
      • 要感谢的人
      • 浅析正题
    • 题目要求
      • 题目
      • 要求
    • 实现思路
      • 数据栈
      • 排位栈
      • 双栈怎么配合
      • 实现的两种方式
    • 我的代码实现
    • 收获与启示

前言

要感谢的人

临近暑假结束的时候,收到一封私信。是来自http://blog.csdn.net/u011068702 博友的。并向我推荐了一本好书。
程序员代码面试指南:IT名企算法与数据结构题目最优解,是左程云学者刷题五年的经验结晶,很赞的一本书。

在此,再次感谢 http://blog.csdn.net/u011068702 博友!

浅析正题

本文主要是对学习了解题思路后的一个延伸及应用,有一个getMin的Stack为基础,实现了一个getMax的Stack。以及从这两个例子中,我学到了一个怎样的思想,一种解决同类问题的思路。

题目要求

题目

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

要求

1、pop,push , getMin 操作的时间复杂度都是O(1)
2、设计的栈类型可以使用现成的栈结构

实现思路

看完了题目以及要求,是不是同样感觉一头雾水?就我自己而言,我首先想到的便是借助一个多维数组来实现,分别记录push的顺序,次数,以及相应的值(当然,是以基数排序或者桶排序的思想来存放),但是题目要求对时间复杂度有明显的要求,显然这就有点 力不从心了。

作者实现的方式很巧妙,那就是以增加附加代价(新增一个栈)的方式来降低时间计算上的维度,从而实现满足要求的时间复杂度!

数据栈

数据栈的存在就是维持正常的栈所具备的pop,以及push操作。当然了这并不是 平常见到的普通的pop,push操作,而是经过了相关逻辑处理的。

排位栈

排位栈最为核心的作用就是实现getMin功能,但是其作为重要的地方就是配合上述的pop,push来完成数据的压入和弹出,来使得排位栈中的数据有序。

双栈怎么配合

关于配合的方式有覆盖型的和非覆型的,不过这并不是本次试验的重点,待会的代码会给你一个更为清晰的认识。下面先上一张手绘的图简单的示意一番吧。

实现的两种方式

现在接着刚才的那个覆盖型以及非覆盖型的问题,其实覆盖不覆盖是需要和pop,push配合的。

非覆盖型的实现

如果value更小或者两者相等,则将value也压入stackMin
如果value中栈顶元素小,则stackMin不压入任何内容
对应取出数据的时候只需要保证双栈栈顶元素一致时双栈pop,否则只有数据栈pop即可,便可以保证最小元素始终保留在排位栈的栈顶。

而覆盖型的实现为

压入的时候和前面一种不同的方式是,如果压入的数据比栈顶元素大,
我们依然压入栈顶元素。
同样,这个时候,我们pop的时候就随意了。因为排位栈中pop出来的肯定是数据栈中存有的元素的最小值。

我的代码实现

代码稍有点长,但是核心算法很简单,就不再做过多的注释了。

/** * @Date 2016年9月6日 * * @author 郭 璞 * */
package stack_and_queue;

import java.util.Map;
import java.util.Stack;

/** * @author 郭 璞 * */
public class BetterStack {
    public static void main(String[] args) {
        NotOverrideMinStack notors = new NotOverrideMinStack();
        notors.push(2);
        notors.push(3);
        notors.push(1);
        notors.push(7);
        notors.push(12);

        System.out.println("最小值为:" + (notors.getMin()));
        System.out.println("栈顶元素的值为:" + notors.pop());
        System.out.println("最小值为:" + (notors.getMin()));
        System.out.println("栈顶元素的值为:" + notors.pop());
        System.out.println("最小值为:" + (notors.getMin()));
        System.out.println("栈顶元素的值为:" + notors.pop());
        System.out.println("最小值为:" + (notors.getMin()));
        System.out.println("栈顶元素的值为:" + notors.pop());
        System.out.println("最小值为:" + (notors.getMin()));
        System.out.println("栈顶元素的值为:" + notors.pop());

        System.out.println("-------------------我是分割线---------------");
        OverrideMinStack ors = new OverrideMinStack();
        ors.push(2);
        ors.push(3);
        ors.push(1);
        ors.push(7);
        ors.push(12);

        System.out.println("最小值为:" + (ors.getMin()));
        System.out.println("栈顶元素的值为:" + ors.pop());
        System.out.println("最小值为:" + (ors.getMin()));
        System.out.println("栈顶元素的值为:" + ors.pop());
        System.out.println("最小值为:" + (ors.getMin()));
        System.out.println("栈顶元素的值为:" + ors.pop());
        System.out.println("最小值为:" + (ors.getMin()));
        System.out.println("栈顶元素的值为:" + ors.pop());
        System.out.println("最小值为:" + (ors.getMin()));
        System.out.println("栈顶元素的值为:" + ors.pop());

        System.out.println("--------------------Min&&Max分割线----------------------");
        NotOverrideMaxStack notorms = new NotOverrideMaxStack();
        notorms.push(2);
        notorms.push(1);
        notorms.push(7);
        notorms.push(3);
        notorms.push(12);

        System.out.println("最大值为: " + notorms.getMax());
        System.out.println("当前栈顶元素为: " + notorms.pop());
        System.out.println("最大值为: " + notorms.getMax());
        System.out.println("当前栈顶元素为: " + notorms.pop());
        System.out.println("最大值为: " + notorms.getMax());
        System.out.println("当前栈顶元素为: " + notorms.pop());
        System.out.println("最大值为: " + notorms.getMax());
        System.out.println("当前栈顶元素为: " + notorms.pop());
        System.out.println("最大值为: " + notorms.getMax());
        System.out.println("当前栈顶元素为: " + notorms.pop());

        System.out.println("--------------------分割线----------------------");
        OverrideMaxStack orms = new OverrideMaxStack();
        orms.push(2);
        orms.push(1);
        orms.push(7);
        orms.push(3);
        orms.push(12);

        System.out.println("最大值为: " + orms.getMax());
        System.out.println("当前栈顶元素为: " + orms.pop());
        System.out.println("最大值为: " + orms.getMax());
        System.out.println("当前栈顶元素为: " + orms.pop());
        System.out.println("最大值为: " + orms.getMax());
        System.out.println("当前栈顶元素为: " + orms.pop());
        System.out.println("最大值为: " + orms.getMax());
        System.out.println("当前栈顶元素为: " + orms.pop());
        System.out.println("最大值为: " + orms.getMax());
        System.out.println("当前栈顶元素为: " + orms.pop());
    }

}

class NotOverrideMinStack {

    private Stack<Integer> dataStack;
    private Stack<Integer> minStack;

    public NotOverrideMinStack() {
        this.dataStack = new Stack<Integer>();
        this.minStack = new Stack<Integer>();
    }

    public void push(int item) {
        if (this.dataStack.size() == 0) {
            this.minStack.push(item);
        } else {
            Integer stack_top_value = this.dataStack.peek();
            if (item < stack_top_value) {
                this.minStack.push(item);
            }
        }
        this.dataStack.push(item);

    }

    public Integer pop() {
        Integer stack_top_value = this.dataStack.peek();
        // if (stack_top_value == minStack.peek()) {
        if (stack_top_value == this.getMin()) {
            this.minStack.pop();
        }
        return this.dataStack.pop();
    }

    public Integer getMin() {
        if (!this.minStack.isEmpty()) {
            return this.minStack.peek();
        }
        return null;
    }
}

class OverrideMinStack {
    private Stack<Integer> dataStack;
    private Stack<Integer> minStack;

    public OverrideMinStack() {
        this.dataStack = new Stack<Integer>();
        this.minStack = new Stack<Integer>();
    }

    public void push(int item) {
        if (this.dataStack.size() == 0) {
            this.minStack.push(item);
        } else {
            this.minStack.push(item < this.minStack.peek() ? item : this.minStack.peek());
        }
        this.dataStack.push(item);

    }

    public Integer pop() {
        if (!this.dataStack.isEmpty()) {
            this.minStack.pop();
            return this.dataStack.pop();
        } else {
            System.out.println("数据栈为空,不能再弹出数据!");
            return -999999;
        }
    }

    public Integer getMin() {
        if (this.minStack.size() > 0) {
            return this.minStack.peek();
        }
        System.out.println("栈中没有数据可以弹出!");
        return null;
    }

}

class NotOverrideMaxStack {

    private Stack<Integer> dataStack;
    private Stack<Integer> maxStack;

    public NotOverrideMaxStack() {
        this.dataStack = new Stack<Integer>();
        this.maxStack = new Stack<Integer>();
    }

    public void push(Integer item) {
        if (this.dataStack.size() == 0) {
            this.maxStack.push(item);
        } else {
            // this.maxStack.push(this.maxStack.peek() > item ?
            // this.maxStack.peek() : item);
            if (this.maxStack.peek() < item) {
                this.maxStack.push(item);
            }
        }
        this.dataStack.push(item);
    }

    public Integer pop() {
        if (!this.maxStack.isEmpty()) {
            if (this.maxStack.peek() == this.dataStack.peek()) {
                this.maxStack.pop();
            }
            return this.dataStack.pop();
        }
        return null;
    }

    public Integer getMax() {
        return this.maxStack.peek();
    }
}

class OverrideMaxStack {

    private Stack<Integer> dataStack;
    private Stack<Integer> maxStack;

    public OverrideMaxStack() {
        this.dataStack = new Stack<Integer>();
        this.maxStack = new Stack<Integer>();
    }

    public void push(Integer item) {
        if (this.dataStack.size() == 0) {
            this.maxStack.push(item);
        } else {
            this.maxStack.push(this.maxStack.peek() >= item ? this.maxStack.peek() : item);
        }
        this.dataStack.push(item);
    }

    public Integer pop() {
        if (!this.dataStack.isEmpty()) {
            this.maxStack.pop();
            return this.dataStack.pop();
        }
        return null;
    }

    public Integer getMax() {
        return this.maxStack.peek();
    }
}

收获与启示

至此,一个增强型的栈变设计完成了。其中作者使用两个栈来削减时间复杂度的巧妙的思路真的是给人一种美感。

那么,这种思路还能运用到哪里呢?

我觉得这可以作为一类问题的通用解法,很多时候,我们的思维会僵化在一个点上,努力了很久却也没有多少突破。这个时候不妨像作者一样,换个思路。牺牲点其他的“代价”,或许就会有不一样的收获。

软件开发过程中也是这样,完成一个项目不是说非要吊死在一棵树上,就目前而言,完成一个功能有很多语言,亦或是同一种语言的不同的方法来实现,当思维聚焦的地方实在是没有突破点的时候,就应该考虑考虑换个思路了。

这也是本例“双栈来增强一个栈”的包装模式的很好的体现。

你可能感兴趣的:(数据结构,算法,栈)