2018-10-06 22:01:11

问题描述:

问题求解:

为每个频率创建一个栈即可。

  1. class FreqStack {
  2. Map<Integer, Integer> map;
  3. List<Stack<Integer>> stacks;
  4.  
  5. public FreqStack() {
  6. map = new HashMap<>();
  7. stacks = new ArrayList<>();
  8. }
  9.  
  10. public void push(int x) {
  11. int freq = map.getOrDefault(x, 0);
  12. freq++;
  13. map.put(x, freq);
  14. if (freq > stacks.size()) {
  15. Stack<Integer> stack = new Stack<>();
  16. stack.push(x);
  17. stacks.add(stack);
  18. }
  19. else {
  20. Stack<Integer> stack = stacks.get(freq - 1);
  21. stack.push(x);
  22. }
  23. }
  24.  
  25. public int pop() {
  26. Stack<Integer> stack = stacks.get(stacks.size() - 1);
  27. int res = stack.pop();
  28. if (stack.isEmpty()) stacks.remove(stacks.size() - 1);
  29. int freq = map.get(res);
  30. map.put(res, --freq);
  31. if (freq == 0) map.remove(res);
  32. return res;
  33. }
  34. }
  35.  
  36. /**
  37. * Your FreqStack object will be instantiated and called as such:
  38. * FreqStack obj = new FreqStack();
  39. * obj.push(x);
  40. * int param_2 = obj.pop();
  41. */

最大频率栈 Maximum Frequency Stack的更多相关文章

  1. [Swift]LeetCode895. 最大频率栈 | Maximum Frequency Stack

    Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...

  2. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  3. [LeetCode] 895. Maximum Frequency Stack 最大频率栈

    Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...

  4. LeetCode - Maximum Frequency Stack

    Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...

  5. Maximum Frequency Stack

    Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...

  6. LeetCode 895. Maximum Frequency Stack

    题目链接:https://leetcode.com/problems/maximum-frequency-stack/ 题意:实现一种数据结构FreqStack,FreqStack需要实现两个功能: ...

  7. Uncaught RangeError: Maximum call stack size exceeded 调试日记

    异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...

  8. Uncaught RangeError: Maximum call stack size exceeded 超出最大调用值(个人解释)

    写了段jq后,报这个错,度娘未解,灵光一闪,找到原因,上代码: Html 结构: <a href="javascript:;" class="item-pic&qu ...

  9. Ext.encode 抛出异常“Uncaught RangeError: Maximum call stack size exceeded”

    在用使用Ext.encode(ExtObject)过程中抛出了如下错误: Uncaught RangeError: Maximum call stack size exceeded 实际上,不能用 E ...

随机推荐

  1. 一个简单的购物金额结算(JAVA)

    我编写的代码: import java.util.Scanner; public class ZuoYe01 { public static void main(String[] args) { // ...

  2. ldap集成zabbix

    zabbix版本:3.0.7 ldap认证配置: zabbix管理员登录-->管理-->认证,选择ldap方式 参照以上格式填写,需注意配置完成后需在zabbix上创建与ldap同名账户才 ...

  3. 20165310 NetSec2019 Week6 Exp4 恶意代码分析

    20165310 NetSec2019 Week6 Exp4 恶意代码分析 一.实验要求 1.系统运行监控 使用如计划任务,每隔一分钟记录自己的电脑有哪些程序在联网,连接的外部IP是哪里.运行一段时间 ...

  4. Spring 学习历程(二)

    JUnit测试 maven导入包 <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> &l ...

  5. SDOI2017相关分析 线段树

    题目 https://loj.ac/problem/2005 思路 \[ \sum_{L}^{R}{(x_i-x)^{2}} \] \[ \sum_{L}^{R}{(x_i^2-2*x_i*x+x^{ ...

  6. 大明A+B(大数相加)解题报告

    Problem Description 话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫"大明". 这时他已经不是那个只会做100以内加法的那个"小明 ...

  7. 题解——code[vs] 1506 传话(传递闭包)

    裸的传递闭包 直接Floyd暴力即可 #include <cstdio> #include <algorithm> #include <cstring> using ...

  8. K8S笔记

    K8S 集群结构图 一些名词: etcd etcd保存了整个集群的状态:用于持久化存储集群中所有的资源对象,如Node.Service.Pod.RC.Namespace等:API Server提供了操 ...

  9. elastic-job的原理简介和使用

    转载:http://blog.csdn.net/fanfan_v5/article/details/61310045 elastic-job是当当开源的一款非常好用的作业框架,在这之前,我们开发定时任 ...

  10. error: could not create '/System/Library/Frameworks/Python.framework/Versions/2.7/share': Operation not permitted

    参考: Python pip安装模块报错 Mac升级到EI Captain之后pip install 无法使用问题 error: could not create '/System/Library/F ...