当前位置:网站首页>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());
}
}
}
}
边栏推荐
- Tp5.0 background admin access
- JS method encapsulation summary
- Experiment 4 CTF practice
- Tp5.1 login configuration method of whether to login public functions (complete login case)
- SQL recursive follow-up
- "Introduction to interface testing" punch in day06: interface testing platform: are tools and frameworks incompatible?
- [stm32f103rct6] motor PWM drive module idea and code
- Domain driven model (DDD)
- Common Oracle commands
- Go multiplexing
猜你喜欢

Mark down learning

Read and upgrade st-link chip information and SWD burning media through STM32 stlink utility tool

Domestic edge computing organization and product research

Vscode configuration, eslint+prettier combined with detailed configuration steps, standardized development

Arduino + si5351 square wave generator

Classic network learning RESNET code implementation
![[stm32f130rct6] idea and code of ultrasonic ranging module](/img/a6/1bae9d5d8628f00acf4738008a0a01.png)
[stm32f130rct6] idea and code of ultrasonic ranging module

Stm32cubemx quadrature encoder

6.0 cancellation of member registration verification code

Resolved (the latest version of selenium reported an error) attributeerror: module 'selenium webdriver‘ has no attribute ‘PhantomJS‘
随机推荐
Wechat H5 record
Arduino IDE for raspberry PI Pico development firmware localization installation tutorial
Class notes (4) (2) -- 572. Compete
"Introduction to interface testing" punch in to learn day09: Micro service interface: how to use mock to solve chaotic call relationships
Pagoda workman WSS reverse proxy socket legal domain name applet chat remove port
Learning notes - talking about the data structure and algorithm of MySQL index and the introduction of index
Bgy development small example
Domain driven model (DDD)
Actual combat in ThreadLocal project
【C】 Advanced knowledge of file operation
JS construct binary tree
Canvas record
JS construction linked list
List title of force buckle summary
mysql_ User table_ Field meaning
Flink's study notes
# CF #808 Div.2(A - C)
Brief understanding of operational amplifier
[stm32f103rct6] can communication
Application method and practical case of sqlmap of penetration test SQL injection