当前位置:网站首页>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(); */
边栏推荐
- LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
- Do you understand automatic packing and unpacking? What is the principle?
- Google browser plug-in recommendation
- Installation and removal of MySQL under Windows
- The data read by pandas is saved to the MySQL database
- [keil5 debugging] warning:enumerated type mixed with other type
- In third tier cities and counties, it is difficult to get 10K after graduation
- 2020-08-23
- Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving
- [combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
猜你喜欢
yocto 技術分享第四期:自定義增加軟件包支持
SCM is now overwhelming, a wide variety, so that developers are overwhelmed
CEF download, compile project
单片机学到什么程度能找到工作,这个标准不好量化
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
在三线城市、在县城,很难毕业就拿到10K
Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~
Which language should I choose to program for single chip microcomputer
2021-10-28
Education is a pass and ticket. With it, you can step into a higher-level environment
随机推荐
Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
在三线城市、在县城,很难毕业就拿到10K
Interruption system of 51 single chip microcomputer
使用sed替换文件夹下文件
Notes on C language learning of migrant workers majoring in electronic information engineering
Oracle database SQL statement execution plan, statement tracking and optimization instance
03 FastJson 解决循环引用
LeetCode - 673. 最长递增子序列的个数
SCM is now overwhelming, a wide variety, so that developers are overwhelmed
QT qcombobox QSS style settings
You need to use MySQL in the opening experiment. How can you forget the basic select statement? Remedy is coming~
Not many people can finally bring their interests to college graduation
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
Screen display of charging pile design -- led driver ta6932
YOLO_ V1 summary
Qcombox style settings
单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
4G module initialization of charge point design