面试手撕代码总结(三)-栈

面试手撕代码总结(三)-栈

1.使用数组实现栈

public class MyStack<E>{
  private Object[] stack;
  private int size;
  public MyStack(){
    stack=new Object[10];
   }
   //判断栈是否为空
  public boolean isEmpty(){
    return size==0;
   }
  public E peek(){
    if(isEmpty()){
     return null;
	}
	return(E) stack[size-1];
  }
  public E pop(){
     E e=peek();
	 stack[size-1]=null;
	 size--;
	 return e;
	}
  public E push(){
    ensureCapacity(size+1);
	stack[size+1]=item;
	return item;
  }
  //判断数组器是否已满,若已满,则扩充数组空间
  private void ensureCapacity(int size){
     int len=stack.length;
	 if(size>len){
	   int newLen=10;
	   stack=Arrays.copyOf(stack,newLen);
	 }
	} 
 }

2.使用链表的方式实现栈

class Node<E>{
	Node<E> next=null;
	E data;
	public Node(E data){this.data=data;}
}
public class Stack<E>{
	 Node<E> top=null;
	 public boolean isEmpty(){
		return top==null;
	 }
	 public void push(E data){
		Node<E> newNode=new Node<E>(data);
		newNode.next=top;
		top=newNode;
	 }
	 public E pop(){
		if(this.isEmpty())
			return null;
		E data=top.data;
		top=top.next;
		return data;
	 }
	 public E peek(){
		if(isEmpty){
			return null;
	   }
	   return top.data;
	 }
}

3.求栈中最小元素

public class MyStack{
   private Stack<Integer> stackData;
   private Stack<Integer> stackMin;
   public MyStack(){
      this.stackData=new Stack<Integer>();
	  this.stackMin=new Stack<Integer>();
	}
   public void push(int newNum){
      if(this.stackMin.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("Your stack is empty");
        }
		int value=this.stackData.pop();
		if(value==this.getMin()){
		   this.stackMin.pop();
		  }
		  return value;
    }
	public int getMin(){
	  if(this.stackMin.isEmpty()){
	        throw new RuntimeException("Your stack is empty");
	   }
	   return this.stackMin.peek();
	   }
}

4.两个栈模拟队列

public class MyQueue<E>{
   private Stack<E> s1=new Stack<E>();
   private Stack<E> s2=new Stack<E>();
   public sychronized void put(E e){
     s1.push(e);
	}
    public sychronized void pop(){
     if(s2.isEmpty()){
	   while(!s1.isEmpty()){
	     s2.push(s1.pop());
	   }
	 }
	 return s2.pop();
	}
	public sychronized boolean empty(){
	   return s1.isEmpty()&&s2.isEmpty();
	}
}

5.判断一个栈是否是另一个栈的弹出顺序

public boolean IsPopOrder(int[] pushA,int[] popA){
  if(pushA==null||popA==null){
    return false;
  }
  Stack<Integer> stack=new Stack<>();
  int index=0;
  for(int i=0;i<pushA.length;i++){
     stack.push(pushA[i]);
	 while(!stack.isEmpty()&&stack.peek()==popA[index]){
	    stack.pop();
		index++;
	  }
	}
	return stack.isEmpty();
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值