最大频率栈 Maximum Frequency Stack
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的更多相关文章
- [Swift]LeetCode895. 最大频率栈 | Maximum Frequency Stack
Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- [LeetCode] 895. Maximum Frequency Stack 最大频率栈
Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...
- LeetCode - Maximum Frequency Stack
Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...
- Maximum Frequency Stack
Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...
- LeetCode 895. Maximum Frequency Stack
题目链接:https://leetcode.com/problems/maximum-frequency-stack/ 题意:实现一种数据结构FreqStack,FreqStack需要实现两个功能: ...
- Uncaught RangeError: Maximum call stack size exceeded 调试日记
异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...
- Uncaught RangeError: Maximum call stack size exceeded 超出最大调用值(个人解释)
写了段jq后,报这个错,度娘未解,灵光一闪,找到原因,上代码: Html 结构: <a href="javascript:;" class="item-pic&qu ...
- Ext.encode 抛出异常“Uncaught RangeError: Maximum call stack size exceeded”
在用使用Ext.encode(ExtObject)过程中抛出了如下错误: Uncaught RangeError: Maximum call stack size exceeded 实际上,不能用 E ...
随机推荐
- 一个简单的购物金额结算(JAVA)
我编写的代码: import java.util.Scanner; public class ZuoYe01 { public static void main(String[] args) { // ...
- ldap集成zabbix
zabbix版本:3.0.7 ldap认证配置: zabbix管理员登录-->管理-->认证,选择ldap方式 参照以上格式填写,需注意配置完成后需在zabbix上创建与ldap同名账户才 ...
- 20165310 NetSec2019 Week6 Exp4 恶意代码分析
20165310 NetSec2019 Week6 Exp4 恶意代码分析 一.实验要求 1.系统运行监控 使用如计划任务,每隔一分钟记录自己的电脑有哪些程序在联网,连接的外部IP是哪里.运行一段时间 ...
- Spring 学习历程(二)
JUnit测试 maven导入包 <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> &l ...
- SDOI2017相关分析 线段树
题目 https://loj.ac/problem/2005 思路 \[ \sum_{L}^{R}{(x_i-x)^{2}} \] \[ \sum_{L}^{R}{(x_i^2-2*x_i*x+x^{ ...
- 大明A+B(大数相加)解题报告
Problem Description 话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫"大明". 这时他已经不是那个只会做100以内加法的那个"小明 ...
- 题解——code[vs] 1506 传话(传递闭包)
裸的传递闭包 直接Floyd暴力即可 #include <cstdio> #include <algorithm> #include <cstring> using ...
- K8S笔记
K8S 集群结构图 一些名词: etcd etcd保存了整个集群的状态:用于持久化存储集群中所有的资源对象,如Node.Service.Pod.RC.Namespace等:API Server提供了操 ...
- elastic-job的原理简介和使用
转载:http://blog.csdn.net/fanfan_v5/article/details/61310045 elastic-job是当当开源的一款非常好用的作业框架,在这之前,我们开发定时任 ...
- 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 ...