2018-10-06 22:01:11

问题描述:

问题求解:

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

class FreqStack {
Map<Integer, Integer> map;
List<Stack<Integer>> stacks; public FreqStack() {
map = new HashMap<>();
stacks = new ArrayList<>();
} public void push(int x) {
int freq = map.getOrDefault(x, 0);
freq++;
map.put(x, freq);
if (freq > stacks.size()) {
Stack<Integer> stack = new Stack<>();
stack.push(x);
stacks.add(stack);
}
else {
Stack<Integer> stack = stacks.get(freq - 1);
stack.push(x);
}
} public int pop() {
Stack<Integer> stack = stacks.get(stacks.size() - 1);
int res = stack.pop();
if (stack.isEmpty()) stacks.remove(stacks.size() - 1);
int freq = map.get(res);
map.put(res, --freq);
if (freq == 0) map.remove(res);
return res;
}
} /**
* Your FreqStack object will be instantiated and called as such:
* FreqStack obj = new FreqStack();
* obj.push(x);
* int param_2 = obj.pop();
*/

最大频率栈 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. Python 操作 mysql数据库的一个小小的基础案例,小白新手,以备后用~~

    model.py 中的代码 # Create your models here. # 书和作者一对多 class Author(models.Model): name = models.CharFie ...

  2. 01:jQuery的下拉选select2插件用法

    1.1 select2插件基本使用 1.下载select2插件 1. 下载地址:https://github.com/select2/select2 2.官网地址:https://select2.or ...

  3. HAProxy实现slave负载均衡[高可用]

    下面要执行的是HAProxy部分 这是一个集群,其他的部分在: mysql-cluster 7.3.5安装部署 mysql主备部署[高可用] mysql主备切换[高可用] mysql读写分离[高可用] ...

  4. 2018年湘潭大学程序设计竞赛G又见斐波那契

    链接:https://www.nowcoder.com/acm/contest/105/G来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  5. STL相关问题

    写set容器遇到以下问题: C:\Users\admin\Desktop\未命名2.cpp In function 'int main()': 67 98 C:\Users\admin\Desktop ...

  6. 在Linux系统上卸载playOnLinux

    1.remove just the playonlinux package itself. sudo apt-get remove playonlinux 2.remove the playonlin ...

  7. Bootstrap3基础 btn-xs/sm... 按钮的四种大小

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  8. 从Hello World说起(Dart)到“几乎所有东西都是Widget”小部件。

    import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class MyApp extends S ...

  9. 快速自动安装dart

    @"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat Non ...

  10. 颠倒的价牌|2013年蓝桥杯A组题解析第四题-fishers

    颠倒的价牌 小李的店里专卖其它店中下架的样品电视机,可称为:样品电视专卖店. 其标价都是4位数字(即千元不等). 小李为了标价清晰.方便,使用了预制的类似数码管的标价签,只要用颜色笔涂数字就可以了(参 ...