当前位置:网站首页>LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
2022-07-03 09:20:00 【三岁就很萌@D】


两个双端队列

class FrontMiddleBackQueue {
//将所有值分为两部分
//前半部分放在deque1
private Deque<Integer> deque1;
//后半部分放在deque2
private Deque<Integer> deque2;
//使deque1的值个数要不与deque2的值个数相等(值个数为偶数) 要不比deque2多一个(值个数为奇数)
public FrontMiddleBackQueue() {
deque1 = new LinkedList<>();
deque2 = new LinkedList<>();
}
public void makeBalance(){
//使deque1的值个数要不与deque2的值个数相等(值个数为偶数) 要不比deque2多一个(值个数为奇数)
//不满足的情况只有两种
//deque1的值个数比deque2的值个数多两个
if(deque1.size() > deque2.size()+1)
deque2.offerFirst(deque1.pollLast());
//deque2的值个数比deque1的值个数多一个
if(deque2.size() > deque1.size())
deque1.offerLast(deque2.pollFirst());
}
public void pushFront(int val) {
//放在deque1的前面
deque1.offerFirst(val);
//维持一下平衡
makeBalance();
}
public void pushMiddle(int val) {
//若现有值为1 3 2
//经过makeBalance deque1 1 3 deque2 2
//此时pushMiddle 4
//但是当值为奇数个时 pushMiddle是放在 3 之前的 所以要先把3 放入deque2 再把 4 放入deque1
// deque1 1 4 deque2 3 2
if(deque1.size() > deque2.size())
deque2.offerFirst(deque1.pollLast());
//若现有值为1 4 3 2
//则deque1 1 4 deque2 3 2
//pushMiddle 5
//直接把5放入deque1
//deque1 1 4 5 deque2 3 2
deque1.offerLast(val);
}
public void pushBack(int val) {
//直接放入deque2 末端
deque2.offerLast(val);
//维持平衡
makeBalance();
}
public int popFront() {
//在deque1不为空的情况下 直接返回deque1首端值
if(deque1.size() != 0){
int val = deque1.pollFirst();
makeBalance();
return val;
}
//因为deque1的值个数一定比deque2值个数多 如果deque1为空的话说明当前值个数为0
else
return -1;
}
public int popMiddle() {
//当值是奇数个
//1 3 5 7 9
// deque1 1 3 5 deque2 7 9
//当值是偶数个
// 1 3 5 7
// deque1 1 3 deque2 5 7
//不管怎么样 middle 都是deque1的末端值
if(deque1.size() != 0){
int val = deque1.pollLast();
makeBalance();
return val;
}
else
return -1;
}
public int popBack() {
//当值个数为0时 deque1 和deque2 都为空时 返回-1
if(deque1.size() == 0 && deque2.size() == 0){
return -1;
}
else if(deque2.size() != 0){
//当值个数大于等于2时 deque2中一定有值 返回 deque2末端值
int val = deque2.pollLast();
makeBalance();
return val;
}else{
// 当值个数为1 时 deque1.size() == 1 deque2.size() == 0 返回deque1的末端值
int val = deque1.pollLast();
makeBalance();
return val;
}
}
}
/** * Your FrontMiddleBackQueue object will be instantiated and called as such: * FrontMiddleBackQueue obj = new FrontMiddleBackQueue(); * obj.pushFront(val); * obj.pushMiddle(val); * obj.pushBack(val); * int param_4 = obj.popFront(); * int param_5 = obj.popMiddle(); * int param_6 = obj.popBack(); */
边栏推荐
- 干单片机这一行的时候根本没想过这么多,只想着先挣钱养活自己
- Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
- Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~
- 手机都算是单片机的一种,只不过它用的硬件不是51的芯片
- Do you understand automatic packing and unpacking? What is the principle?
- MySQL 数据库基础知识(系统化一篇入门)
- Not many people can finally bring their interests to college graduation
- Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
- Yocto technology sharing phase IV: customize and add software package support
- JS基础-原型原型链和宏任务/微任务/事件机制
猜你喜欢

QT is a method of batch modifying the style of a certain type of control after naming the control

Which language should I choose to program for single chip microcomputer

yocto 技術分享第四期:自定義增加軟件包支持

Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()

03 FastJson 解决循环引用

Oracle database SQL statement execution plan, statement tracking and optimization instance

Installation and removal of MySQL under Windows

LeetCode - 919. 完全二叉树插入器 (数组)

Gpiof6, 7, 8 configuration

Timer and counter of 51 single chip microcomputer
随机推荐
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
My notes on intelligent charging pile development (II): overview of system hardware circuit design
Fundamentals of Electronic Technology (III)__ Chapter 6 combinational logic circuit
03 fastjason solves circular references
When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
Installation and removal of MySQL under Windows
Education is a pass and ticket. With it, you can step into a higher-level environment
Liquid crystal display
I didn't think so much when I was in the field of single chip microcomputer. I just wanted to earn money to support myself first
Project scope management__ Scope management plan and scope specification
My openwrt learning notes (V): choice of openwrt development hardware platform - mt7688
2021-10-27
Pymssql controls SQL for Chinese queries
Sending and interrupt receiving of STM32 serial port
STM32 running lantern experiment - library function version
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
4G module initialization of charge point design
Adaptiveavgpool1d internal implementation
2021-11-11 standard thread library
Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving