当前位置:网站首页>Using one stack to sort another stack
Using one stack to sort another stack
2022-07-25 03:01:00 【Rookies also have dreams】
subject :
The type in a stack is integer , Now I want to sort the stack from top to bottom in order from small to large , Only one stack is allowed besides , You can apply for new variables , But you can't apply for additional data structures , How to sort .
Ideas :
To design a cur Variable , Deposit stack The current element popped from the stack , And auxiliary stack help Compare the top elements of the stack , If it is larger than the top element of the auxiliary stack , Then pop up the elements in the auxiliary stack one by one , Push the stack In the stack , If it is smaller than the top element of the auxiliary stack , Will cur Push into auxiliary stack , know stack The stack is empty. , Then pop up and press the auxiliary stack one by one stack In the stack , Sort complete .
public class test1 {
public void sortStack() {
Stack<Integer> help = new Stack<>();
Stack<Integer> stack = new Stack<>();
while (!stack.isEmpty()) {
int cur = stack.pop();
while (!help.isEmpty() && cur > help.peek()) {
stack.push(help.pop());
}
help.push(cur);
while (!help.isEmpty()){
stack.push(help.pop());
}
}
}
}
边栏推荐
- Conceptual distinction between Po, Bo, VO, dto and POJO
- [pyGame practice] nostalgic classic - do you remember the name of this chess game for children? (must collect)
- Tensorflow's study notes (I)
- Tp5.1 initialize initialization method (not \u initialize)
- Actual combat in ThreadLocal project
- It7259q-13, it7259ex-24 feature wearable devices
- Color space (2) -- YUV
- Experiment 4 CTF practice
- Dynamic planning of force buckle punch in summary
- Several dpdk control frameworks
猜你喜欢

2022-07-19: all factors of F (I): I are added up after each factor is squared. For example, f (10) = 1 square + 2 square + 5 square + 10 square = 1 + 4 + 25 + 100 = 130.

JS written test -- regular expression

Stm32cubemx quadrature encoder

Classic network learning RESNET code implementation

Do you know about real-time 3D rendering? Real time rendering software and application scenarios are coming

Resolve the error: org.apache.ibatis.binding.bindingexception
![[jailhouse article] scheduling policies and system software architectures for mixed criticality](/img/cc/874f8436991fdeb3a0e87e526c1753.png)
[jailhouse article] scheduling policies and system software architectures for mixed criticality

FLASH read / write problem of stm32cubemx

Win10 -- open the hosts file as an administrator

Error: tomee required to support ear/ejb deployment
随机推荐
List title of force buckle summary
Backtracking to solve combinatorial problems
Learning record 12
Error: tomee required to support ear/ejb deployment
Color space (2) -- YUV
Arduino + si5351 square wave generator
Hashcode details
JS method encapsulation summary
Several dpdk control frameworks
JS foundation -- regular expression
Solve ''_ Xsrf 'argument missing from post
Review all frames before sum of SSM frames
Bgy development small example
Method of adding kernel in Jupiter notebook
Define macros in makefile and pass them to source code
Study notes of filebeat
Decoding webp static pictures using libwebp
Classic network learning RESNET code implementation
[jailhouse article] scheduling policies and system software architectures for mixed criticality
Interview question -- event cycle