1数组栈
2动态数组栈
动态数组栈有两种实现方法,一种是每次扩充1位并进行复制,插入n个元素的时间复杂度位O(n^2)--1+2+...+n
一种是每次扩充2倍并进行复制,插入n个元素的时间复杂度为0(n)...1+2+4+8...+n,此处使用这种最优化的方案。
3链表栈
4最小栈的优化版
5栈的逆置(只用push和top)
6数组跨度
7数组跨度2
8栈排序
9数组最大面积
class 数组栈{ //栈顶指针,容量,数组 private int top; private int capacity; private int[] array; public 数组栈(){ capacity=1; array=new int[capacity]; top=-1; } public boolean isEmpty(){ return top==-1; } public boolean isStackFull(){ return top==capacity-1; } public void push(int data){ if (isStackFull()){ System.out.println("over flow"); }else { array[top++]=data; } } public int pop(){ if (isEmpty()){ System.out.println("empty"); return 0; }else { return array[top--]; } } public void delete(){ top=-1; } }
class 动态数组栈{ int capcity; int top; int[] array; 动态数组栈(){ capcity=1; top=-1; array=new int[capcity]; } public boolean isEmpty(){ return top==-1; } public boolean isStackFull(){ return top==capcity-1; } public void push(int data){ if (isStackFull()) { doubleStack(); } array[top++]=data; } public int pop(){ if (isEmpty()){ System.out.println("empty"); } return array[top--]; }//模拟数组的扩容,进行二倍扩容 public void doubleStack(){ int[] newArray = new int[capcity*2]; for (int i=0;i<capcity;i++){ newArray[i++]=array[i++]; } capcity=capcity*2; array=newArray; } public void delete(){ top=-1; } }
class 链表栈{//链表栈的优点是不用维护多种指针,容量扩充简单,利用头插法建立链表,出栈的时候返回头节点 LLNode head ; 链表栈(){ head=new LLNode(); } public void push(int data){ LLNode node = new LLNode(); node.val=data; node.next=head; head=node; } public int pop(){ if (!isEmpty())return head.val; else { System.out.println("empty"); return 0; } } public boolean isEmpty(){ return head==null; } public int top(){ if (isEmpty()){ System.out.println("empty"); return 0; } else return head.val; } public void delete(){ head=null; } }
class 最小栈 extends Stack { private Stack<Integer> stack = new Stack(); private Stack<Integer> min = new Stack(); public void push(int data){ stack.push(data); if (min.isEmpty()||min.peek()>=data){//降低空间复杂度,min栈判断再入栈 min.push(data); } } public Integer pop(){ if (stack.isEmpty())return 0; int data = stack.pop(); if (data==min.peek())min.pop();//相等的时候,min才出栈 return data; } public int getMinimum(){ return min.peek(); } }
class 栈逆置{//利用递归,依次出栈,进行如栈操作,如栈操作使用递归,依次压入栈底 public void reverseStack(Stack<Integer> stack){ if (stack.isEmpty())return; int data = stack.pop(); reverseStack(stack); insertStack(stack,data); } private void insertStack(Stack<Integer> stack, int data) { if (stack.isEmpty()){ stack.push(data); return; } int tem = stack.pop(); insertStack(stack,data); stack.push(tem); } }
class 跨度计算{// @Test public void test1(){ int[] array =new int[]{6,1,2,5,4}; int[] newArray = findSpan(array); for (int i:newArray){ System.out.println(i); } } int[] findSpan(int[] array){ int[] spans = new int[array.length]; //Stack<Integer> stack = new Stack<>(); //int p=0; for (int i=0;i<array.length;i++){ int span = 1; int j =i-1; while (j>=0&&array[j]<=array[j+1]){ span++; j--; } spans[i]=span; } return spans; } }
class 跨度计算2{//查找比自己大的最近的元素的位置,stack存放的是位置。 int[] findSpans(int[] array){ int[] spans = new int[array.length]; Stack<Integer> stack = new Stack<>(); int p =0; for (int i=0;i<array.length;i++){ while (!stack.isEmpty()&&array[i]>array[stack.peek()]){//最多执行n次,时间总复杂度为O(N) stack.pop(); } if (stack.isEmpty())p=-1; else p=stack.pop(); spans[i]=i-p; stack.push(i); } return spans; } }
class 栈排序{//在s和r种进行反复的调整 public Stack<Integer> sort(Stack<Integer> s){ Stack<Integer> r = new Stack<>(); while (!s.isEmpty()){ int tmp = s.pop(); while (!r.isEmpty()&&r.peek()>tmp){//如果r的顶元素大于s的顶元素(已出栈),则回退压栈 s.push(r.pop()); } r.push(tmp); } return r; } }
class 最大面积{//暴力法三层for循环可以解决。利用栈时间复杂的为o(n),栈维护的是递增的序列的下标。 int largestRectangleArea(int[] height) { int res=0; Stack<Integer> stk=new Stack<>(); //stk.push(0); for(int i=0;i<height.length;++i) { if(stk.empty()||height[stk.peek()]<=height[i]) stk.push(i); else { int temp=stk.peek(); stk.pop(); res=Math.max(res,height[temp]*(stk.empty()?i:(i-stk.peek()-1)));//这步操作很关键,如果stk是空的话,乘以i --i; } } return res; } }