当前位置:网站首页>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(); */
边栏推荐
- Gpiof6, 7, 8 configuration
- Leetcode 300 最长上升子序列
- Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
- 2. Elment UI date selector formatting problem
- Crash工具基本使用及实战分享
- Blue Bridge Cup for migrant workers majoring in electronic information engineering
- Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
- Basic knowledge of MySQL database (an introduction to systematization)
- There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
- The data read by pandas is saved to the MySQL database
猜你喜欢

LeetCode - 706 设计哈希映射(设计) *

YOLO_ V1 summary

yocto 技术分享第四期:自定义增加软件包支持

应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机

LeetCode - 705 设计哈希集合(设计)

2021-10-27

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

Yocto Technology Sharing Phase 4: Custom add package support

Vgg16 migration learning source code

Dictionary tree prefix tree trie
随机推荐
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
Yocto Technology Sharing Phase 4: Custom add package support
LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
Interruption system of 51 single chip microcomputer
2021-11-11 standard thread library
I think all friends should know that the basic law of learning is: from easy to difficult
应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机
Liquid crystal display
(2) New methods in the interface
MySQL root user needs sudo login
Which language should I choose to program for single chip microcomputer
[keil5 debugging] warning:enumerated type mixed with other type
Drive and control program of Dianchuan charging board for charging pile design
4G module board level control interface designed by charging pile
A lottery like scissors, stone and cloth (C language)
Basic knowledge of communication interface
2.Elment Ui 日期选择器 格式化问题
Quelle langue choisir pour programmer un micro - ordinateur à puce unique