当前位置:网站首页>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());
}
}
}
}
边栏推荐
- [pyGame practice] nostalgic classic - do you remember the name of this chess game for children? (must collect)
- Matlab for circular pit
- Decoding webp static pictures using libwebp
- 【C】 Advanced knowledge of file operation
- Solve ''_ Xsrf 'argument missing from post
- Bgy development small example
- Go multiplexing
- Sum of "n" numbers of force deduction summary
- Brief understanding of operational amplifier
- FLASH read / write problem of stm32cubemx
猜你喜欢
![[jailhouse article] certify the uncertified rewards assessment of virtualization for mixed criticality](/img/12/1763571a99e6ef15fb7f9512c54e9b.png)
[jailhouse article] certify the uncertified rewards assessment of virtualization for mixed criticality

Use of stm32cubemonitor part I - data plotting and instrument display

Pagoda workman WSS reverse proxy socket legal domain name applet chat remove port

Tp5.1 initialize initialization method (not \u initialize)

IO (1) -io layering

【C】 Advanced knowledge of file operation

Dynamic planning of force buckle punch in summary

Resolved (the latest version of selenium reported an error) attributeerror: module 'selenium webdriver‘ has no attribute ‘PhantomJS‘

Learning notes - talking about the data structure and algorithm of MySQL index and the introduction of index

Decoding webp static pictures using libwebp
随机推荐
Technical experts from large factories: Thoughts on cloud nativity and software supply chain security
mysql_ Create temporary table
[Kali's sshd service is enabled]
Several dpdk control frameworks
Experiment 4 CTF practice
Matlab for circular pit
Daily three questions 7.19
Edit mathematical formulas in markdown
Define macros in makefile and pass them to source code
YouTube Download and (batch) Download
Riotboard development board series notes (6) -- buildreoot building system image
Daily three questions 7.15
Go common standard library -time
JS interview question - what is the difference between Es5 and ES6?
Clothing ERP | ten advantages of clothing ERP for enterprises
Ffmpeg 4.3 add custom demuxer
Operator explanation - C language
IO (1) -io layering
Domain driven model (DDD)
[jailhouse article] scheduling policies and system software architectures for mixed criticality