当前位置:网站首页>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(); */
边栏推荐
- Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
- Do you understand automatic packing and unpacking? What is the principle?
- Exception handling of arm
- Notes on C language learning of migrant workers majoring in electronic information engineering
- Liquid crystal display
- Swing transformer details-2
- Drive and control program of Dianchuan charging board for charging pile design
- Project scope management__ Scope management plan and scope specification
- Idea remote breakpoint debugging jar package project
- Application of external interrupts
猜你喜欢

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

LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*

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

STM32 interrupt switch

My notes on the development of intelligent charging pile (III): overview of the overall design of the system software

2.Elment Ui 日期选择器 格式化问题

Development of intelligent charging pile (I): overview of the overall design of the system

I think all friends should know that the basic law of learning is: from easy to difficult

Comment la base de données mémoire joue - t - elle l'avantage de la mémoire?

Vgg16 migration learning source code
随机推荐
要选择那种语言为单片机编写程序呢
Timer and counter of 51 single chip microcomputer
Working mode of 80C51 Serial Port
4G module designed by charging pile obtains signal strength and quality
Wireshark use
Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
RESNET code details
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
How does the memory database give full play to the advantages of memory?
Not many people can finally bring their interests to college graduation
Notes on C language learning of migrant workers majoring in electronic information engineering
一个可执行的二进制文件包含的不仅仅是机器指令
Stm32f407 key interrupt
Serial port programming
JS基础-原型原型链和宏任务/微任务/事件机制
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
2021-11-11 standard thread library
JS foundation - prototype prototype chain and macro task / micro task / event mechanism
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)