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. hibernate validator自定义校验注解以及基于服务(服务组)的校验

    hibernate validator是Bean Validation 1.1 (JSR 349) Reference Implementation,其广泛的应用在mvc的参数校验中,尤其是使用服务端 ...

  2. Elasticsearch.Net使用(一)【入门篇】

    http://blog.csdn.net/wulex/article/details/52138564 加数据 //在调用下面的index方法的时候,如果没有指定使用哪个index,ElasticSe ...

  3. Springbooot +Mybaties 配置数据库多数据源

    前言 在实际项目中,我们可能会碰到在一个项目中会访问多个数据库的情况.针对这种情况,我们就需要配置动态的数据源了.一般按照以下步骤即可 一.在启动类上添加注解 二.在application.prope ...

  4. android 文本框不获取焦点的两种方式

    当进入一个页面以后,我们不希望EditText获取焦点自动弹出软键盘,占据大半个屏幕. 方法一 让LinearLayout率先获取焦点,代码如下: <LinearLayout android:f ...

  5. DOM元素加载之前执行的jQuery代码

    <script type="text/javascript"> (function() { alert("DOM还没加载哦!"); })(jQuer ...

  6. SVM学习笔记1-问题定义

    问题定义: 给出一些样本,包含两类.svm试图找到一个超平面,将数据分开,并且每种样本到超平面的距离的最小值最大. 输入样本:$\{x_{i},y_{i}| 1\leq i\leq n \}$,$y_ ...

  7. SDOI2017相关分析 线段树

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

  8. (转)How Hash Algorithms Work

    本文转自:http://www.metamorphosite.com/one-way-hash-encryption-sha1-data-software   Home Posted: Novembe ...

  9. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

  10. latex 脚注编号也成为超链接

    我们用LaTeX写文章时,往往会引用tabularx和hyperref两个包,当我们想让脚注编号也成为超链接以方便阅读时,往往会发现在hyperref包的属性里设置hyperfootnotes=tru ...