当前位置:网站首页>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());
}
}
}
}
边栏推荐
- Keras load history H5 format model error: attributeerror 'STR' object has no attribute 'decode‘
- Ffmpeg 4.3 add custom demuxer
- SQL recursive follow-up
- Operator explanation - C language
- [stm32f130rct6] idea and code of ultrasonic ranging module
- The latest interview questions and analysis of software testing in 2022
- Dc-2-range practice
- The dolphin scheduler calls the shell script and passes multiple parameters
- Execution methods with static code blocks and child and parent classes
- Pypi counts the number of Downloads
猜你喜欢

JS written test questions -- random numbers, array de duplication

Mgre.hdlc.ppp.chap.nat comprehensive experiment

C: wechat chat software instance (wpf+websocket+webapi+entityframework)

Riotboard development board series notes (VIII) -- building desktop system

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

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

Experiment 4 CTF practice

JS foundation -- JSON

Strategy mode, just read one article

@Retryable @backoff @recover retry the use of annotations
随机推荐
DOM operation -- get elements and nodes
Details of happens before rules
Tp5.1 include include files (reference public files)
Wechat sports field reservation of the finished works of the applet graduation project (7) mid-term inspection report
Flume's study notes
Hyperchain hyperblockchain Shi Xingguo was interviewed by 36 krypton: the amount of customer cooperation consulting is doubling
Get to know string thoroughly
Jenkins plug-in development -- plug-in expansion
Request and response
Application method and practical case of sqlmap of penetration test SQL injection
JS foundation -- JSON
JS foundation -- regular expression
Learning Record V
Pypi counts the number of Downloads
Map set learning
[jailhouse article] scheduling policies and system software architectures for mixed criticality
"Introduction to interface testing" punch in day06: interface testing platform: are tools and frameworks incompatible?
"Introduction to interface testing" punch in to learn day05: how can a testing framework support restful interfaces?
List title of force buckle summary
JS written test questions -- random numbers, array de duplication