当前位置:网站首页>Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
2022-07-03 10:06:00 【Cute at the age of three @d】
Two double ended queues
class FrontMiddleBackQueue {
// Divide all values into two parts
// The first half is placed in deque1
private Deque<Integer> deque1;
// The second half is placed in deque2
private Deque<Integer> deque2;
// send deque1 The number of values of is not equal to deque2 The number of values of is equal ( The number of values is even ) Not compare deque2 More than a ( The number of values is odd )
public FrontMiddleBackQueue() {
deque1 = new LinkedList<>();
deque2 = new LinkedList<>();
}
public void makeBalance(){
// send deque1 The number of values of is not equal to deque2 The number of values of is equal ( The number of values is even ) Not compare deque2 More than a ( The number of values is odd )
// There are only two kinds of dissatisfaction
//deque1 The number ratio of values deque2 The number of values is two more
if(deque1.size() > deque2.size()+1)
deque2.offerFirst(deque1.pollLast());
//deque2 The number ratio of values deque1 The number of values is one more
if(deque2.size() > deque1.size())
deque1.offerLast(deque2.pollFirst());
}
public void pushFront(int val) {
// Put it in deque1 In front of
deque1.offerFirst(val);
// Maintain a balance
makeBalance();
}
public void pushMiddle(int val) {
// If the existing value is 1 3 2
// after makeBalance deque1 1 3 deque2 2
// here pushMiddle 4
// But when the value is an odd number pushMiddle It's on the 3 Previous So we need to 3 Put in deque2 And then 4 Put in deque1
// deque1 1 4 deque2 3 2
if(deque1.size() > deque2.size())
deque2.offerFirst(deque1.pollLast());
// If the existing value is 1 4 3 2
// be deque1 1 4 deque2 3 2
//pushMiddle 5
// Put... Directly 5 Put in deque1
//deque1 1 4 5 deque2 3 2
deque1.offerLast(val);
}
public void pushBack(int val) {
// Put it directly in deque2 end
deque2.offerLast(val);
// Keep the balance
makeBalance();
}
public int popFront() {
// stay deque1 Not empty Go straight back to deque1 Head end value
if(deque1.size() != 0){
int val = deque1.pollFirst();
makeBalance();
return val;
}
// because deque1 The number of values of must be greater than deque2 It's worth a lot If deque1 If it is empty, the current number of values is 0
else
return -1;
}
public int popMiddle() {
// The current value is an odd number
//1 3 5 7 9
// deque1 1 3 5 deque2 7 9
// When the value is even
// 1 3 5 7
// deque1 1 3 deque2 5 7
// Anyway middle All are deque1 End value of
if(deque1.size() != 0){
int val = deque1.pollLast();
makeBalance();
return val;
}
else
return -1;
}
public int popBack() {
// When the number of values is 0 when deque1 and deque2 It's all empty time return -1
if(deque1.size() == 0 && deque2.size() == 0){
return -1;
}
else if(deque2.size() != 0){
// When the number of values is greater than or equal to 2 when deque2 There must be value in return deque2 End value
int val = deque2.pollLast();
makeBalance();
return val;
}else{
// When the number of values is 1 when deque1.size() == 1 deque2.size() == 0 return deque1 End value of
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(); */
边栏推荐
- is_ power_ of_ 2 judge whether it is a multiple of 2
- 当你需要使用STM32某些功能,而51实现不了时, 那32自然不需要学
- Education is a pass and ticket. With it, you can step into a higher-level environment
- Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
- pycharm 无法引入自定义包
- 2. Elment UI date selector formatting problem
- Leetcode 300 最长上升子序列
- 01仿B站项目业务架构
- There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
- For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
猜你喜欢
LeetCode - 705 设计哈希集合(设计)
Oracle database SQL statement execution plan, statement tracking and optimization instance
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
SCM career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers
LeetCode - 919. 完全二叉树插入器 (数组)
Octave instructions
编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解
当你需要使用STM32某些功能,而51实现不了时, 那32自然不需要学
随机推荐
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
El table X-axis direction (horizontal) scroll bar slides to the right by default
Leetcode bit operation
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
Circular queue related design and implementation reference 1
Gpiof6, 7, 8 configuration
Octave instructions
Screen display of charging pile design -- led driver ta6932
Installation and removal of MySQL under Windows
LeetCode - 706 设计哈希映射(设计) *
QT setting suspension button
About windows and layout
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
03 fastjason solves circular references
getopt_ Typical use of long function
QT detection card reader analog keyboard input
The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
(2) New methods in the interface
03 fastjason solves circular references