剑指offer

剑指 Offer 30. 包含min函数的栈

class MinStack {
		 Stack<Integer> data;
		 Stack<Integer> min;
		 public MinStack() {
			 data = new Stack<>();//本段代码只能写在构造函数里
			 min = new Stack<>();
		 }
	     public void push(int x) {
	    	 data.push(x);
	    	 if(min.isEmpty() || min.peek() >= x){
	    		 min.push(x);
	    	 }
	    	 //也可只保存当前的最小值,在pop时判断
	     }
	    
	     public void pop() {
	        if(min.peek() == data.peek())
	        	min.pop();
	        data.pop();
	        
	     }
	    
	     public int top() {
	    	 return data.peek();
	     }
	    
	     public int min() {
	    	 return min.peek();
	     }
	 
}

面试题 16.25. LRU 缓存

	class LRUCache {
		int capacity;
		HashMap<Integer, Integer> map;
		LinkedList<Integer> list;

	    public LRUCache(int capacity) {
	    	map = new HashMap<>();
	    	this.capacity = capacity;
	    	list = new LinkedList<>();
	    }
	    
	    public int get(int key) {
	    	if(!map.containsKey(key)){
	    		return -1;
	    	}else{
	    		list.remove((Integer)key);
	    		list.addLast(key);
	    		return map.get(key);
	    	}
	    }
	    

	    public void put(int key, int value) {
	    	if(!map.containsKey(key)){
	    		if(list.size() < capacity){
	    			//注意时小于号,因为等于时容量已经达到上限,不能再放入
	    			list.addLast(key);
	    			map.put(key, value);
	    		}else{
	    			//注意,remove时,map和list都要remove,并且需要先
	    			//remove map,否则不知道应该remove哪个
	    			map.remove(list.peekFirst());
	    			list.removeFirst();
	    			
	    			map.put(key, value);
	    			list.addLast(key);
	    		}
	    	}else{
	    		list.remove((Integer)key);
	    		list.addLast(key);
	    		map.put(key, value);
	    		//注意新放入的密码可能会变,所以需要再put一次map
	    	}
	    }
	}

剑指 Offer 09. 用两个栈实现队列

class CQueue {
		Stack<Integer> A;
		Stack<Integer> B;
	    public CQueue() {
	    	A = new Stack<>();
	    	B = new Stack<>();
	    }
	    
	    public void appendTail(int value) {
	    	A.push(value);

	    }
	    
	    public int deleteHead() {
	    //判断是否为空
	    	if(!B.empty())
	    		return B.pop();
	    	else if(A.empty())
	    		return -1;
	    	else {
				while(!A.empty()){
					int a  = A.pop();
					B.push(a);
				}
				return B.pop();
			}

	    }
	}

225. 用队列实现栈

class MyStack {
	    Queue<Integer> q1;
	    Queue<Integer> q2;
	    public MyStack() {
	    	q1 = new LinkedList<>();
	    	q2 = new LinkedList<>();
	    }
	    
	    /** Push element x onto stack. */
	    public void push(int x) {
	    	q2.offer(x);//q2接住新节点
	    	//旧的节点依次进来,新的在最前面,所以出队会先出
	    	while(!q1.isEmpty()){
	    		q2.offer(q1.poll());
	    	}
	    	//交换,q2继续当辅助队列。
	    	Queue<Integer> qtemp = q2;
	    	q2 = q1;
	    	q1 = qtemp;

	    }
	    
	    /** Removes the element on top of the stack and returns that element. */
	    public int pop() {
	    	return q1.poll();

	    }
	    
	    /** Get the top element. */
	    public int top() {
	    	return q1.peek();

	    }
	    
	    /** Returns whether the stack is empty. */
	    public boolean empty() {
	    	return q1.isEmpty();
	    }
	}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值