数据结构与算法总结(三)

106 阅读1分钟
  • 题目:实现一个特殊的栈,在基本功能的基础上,再实现返回栈中最小元素的功能
    • 思路:新加一个栈,栈中管理最小值,每次push的时候会和栈顶数据比较并存储最小值
        class MyMinStack {
        private Stack<Integer> minStack;

        private Stack<Integer> stack;

        public MyMinStack() {
            minStack = new Stack<>();
            stack = new Stack<>();
        }

        public void push(Integer i) {
            stack.push(i);
            if (!minStack.isEmpty()) {
                Integer peek = minStack.peek();
                minStack.push(Math.min(peek, i));
            } else {
                minStack.push(i);
            }
        }


        public Integer pop() {
            if (!stack.isEmpty()) {
                minStack.pop();
                return stack.pop();
            } else {
                throw new RuntimeException("栈中没有数据了");
            }
        }

        public Integer getMin() {
            if (!minStack.isEmpty()) {
                return minStack.peek();
            } else {
                throw new RuntimeException("栈中没有数据了");
            }
        }
    }
  • 题目二:使用栈实现队列
    • 思路:使用两个栈,负负得正,注意数据只能全部push完再pop
public class StackToQueue {

    static class MyQueue<T> {
        //push栈
        private Stack<T> stack1;
        //pop栈
        private Stack<T> stack2;

        public MyQueue() {
            stack1 = new Stack<T>();
            stack2 = new Stack<T>();
        }

        public void add(T value) {
            stack1.push(value);
            pushToPop();
        }


        private void pushToPop() {
            if (stack2.isEmpty()) {
                while (!stack1.isEmpty()) {
                    stack2.push(stack1.pop());
                }
            }
        }

        public T poll() {
            if(isEmpty()){
                throw new RuntimeException("队列数据为空");
            }
            pushToPop();
            return stack2.pop();

        }

        public boolean isEmpty() {
            return stack2.isEmpty() && stack1.isEmpty();
        }
        
        public T peek(){
            if(isEmpty()){
                throw new RuntimeException("队列数据为空");
            }
            pushToPop();
            return stack2.peek();
        }

    }


   


}

  • 使用队列实现栈
    • 两个队列相互交替,每次只保留一个数
public class QueueToStack {

    static class MyStack<T> {
        private Queue<T> queue;
        private Queue<T> help;


        public MyStack() {
            queue = new LinkedList<>();
            help = new LinkedList<>();
        }

        public void push(T value) {
            queue.add(value);
        }

        public T pop() {
            if (!queue.isEmpty()) {
                while (queue.size() > 1) {
                    help.add(queue.poll());
                }

                T poll = queue.poll();
                Queue<T> tmp = queue;
                queue = help;
                help = tmp;
                return poll;
            } else {
                throw new RuntimeException("暂无数据");
            }


        }


        public T peek() {
            if (!queue.isEmpty()) {
                while (queue.size() > 1) {
                    help.add(queue.poll());
                }

                T poll = queue.poll();
                help.add(poll);
                Queue<T> tmp = queue;
                queue = help;
                help = tmp;
                return poll;
            } else {
                throw new RuntimeException("暂无数据");
            }
        }

        public boolean isEmpty() {
            return queue.isEmpty();
        }


    }


  

}

  • 递归(利用系统栈实现)
Master公式
递归形如:T(N) = a*O(N/b)+O(N^d)
如果 log(b,a)> d 复杂度为 O(N^log(b,a))
如果 log(b,a)< d 复杂度为 O(N^d)
如果 log(b,a)=d,复杂度 O(N^d * logN)