DAY11-栈和队列

重点:
  • leetcode239:需要构造单调队列,所以要用到双端队列LinkedList,每次从队尾开始遍历把比自己小的元素都push掉。
  • leetcode347:熟悉优先队列priorityQueue,以及如何创建comparator对象,重写compare方法。

LeetCode150. 逆波兰表达式求值

    public static int evalRPN(String[] tokens) {
    	Deque<Integer> stack = new ArrayDeque<>();
    	for(int i=0;i<tokens.length;i++) {
    		if(tokens[i].equals("+")) {
    			Integer num2 = stack.pop();
    			Integer num1 = stack.pop();
    			stack.push(num1+num2);
    		}else if(tokens[i].equals("-")) {
    			Integer num2 = stack.pop();
    			Integer num1 = stack.pop();
    			stack.push(num1-num2);
    		}else if(tokens[i].equals("*")) {
    			Integer num2 = stack.pop();
    			Integer num1 = stack.pop();
    			stack.push(num1*num2);
    		}else if(tokens[i].equals("/")) {
    			Integer num2 = stack.pop();
    			Integer num1 = stack.pop();
    			stack.push(num1/num2);
    		}else {
    			stack.push(Integer.valueOf(tokens[i]));
    		}
    	}
    	return stack.pop();
    }

LeetCode239. 滑动窗口最大值

	//单调队列
	private static LinkedList<Integer> queue = new LinkedList<>();
	
	//重写pop方法,当头元素为数组所pop的元素再进行pop
	public static void popQueue(int k) {
		if(!queue.isEmpty() &&k==queue.peek()) {
			queue.remove();		
		}
	}
	//重写push方法,维护队头元素为当前最大的元素并且需要保持单调递减
	public static void pushQueue(int k) {
		while(!queue.isEmpty() && queue.getLast()<k) {
			queue.removeLast();
		}
		queue.add(k);
	}
	
    public static int[] maxSlidingWindow(int[] nums, int k) {
    	int[] res = new int[nums.length-k+1];
    	//初始化
    	for(int i=0;i<k;i++) {
    		pushQueue(nums[i]);
    	}
    	res[0]=queue.peek();
    	//进入循环
    	for(int i=1;i<res.length;i++) {
    		popQueue(nums[i-1]);
    		pushQueue(nums[i+k-1]);
    		res[i]=queue.peek();
    	}
    	
    	return res;
    }

LeetCode347.前 K 个高频元素

    public static int[] topKFrequent(int[] nums, int k) {
    	//hashMap存储键值对
    	Map<Integer,Integer> hash = new HashMap<>();
    	for(int i=0;i<nums.length;i++) {
    		hash.put(nums[i], hash.getOrDefault(nums[i], 0)+1);
    	}
    	//建立优先队列
    	PriorityQueue<int[]> queue = new PriorityQueue<int[]>(new Comparator<int[]>() {
    		public int compare(int[] m, int[] n) {
                return m[1] - n[1];
            }
    	});
    	//遍历hash键值对
    	for(Map.Entry<Integer, Integer> entry : hash.entrySet()) {
    		int key = entry.getKey(),value = entry.getValue();
    		if(queue.size()<k) {
    			queue.offer(new int[] {key,value});
    		}else {
    			//判断是否比最小的堆顶要小
    			if(queue.peek()[1]<value) {
    				queue.poll();
    				queue.offer(new int[] {key,value});
    			}
    		}
    	}
    	//记录
    	int[] res = new int[k];
    	for(int i=k-1;i>=0;i--) {
    		res[i]=queue.poll()[0];
    	}
    	return res;
    }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值