当前位置:网站首页>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());
}
}
}
}
边栏推荐
- Arduino IDE for raspberry PI Pico development firmware localization installation tutorial
- Banana pie bpi-m5 toss record (2) -- compile u-boot
- Use unicloud cloud function to decode wechat motion steps in applet
- New key points of ES6
- Hashcode details
- "Introduction to interface testing" punch in day08: can you save all parameters to excel for test data?
- Operator explanation - C language
- PHP record
- Mid year summary and personal feelings
- Matlab for circular pit
猜你喜欢

Wechat sports field reservation of the finished works of the applet graduation project (5) assignment

JS written test question -- deep copy of object

JS construct binary tree

Tensorflow's study notes (I)

Use of stm32cubemonitor part I - data plotting and instrument display

Eslint error

Use pytest + allure to show the chart results (3)

Review all frames before sum of SSM frames

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

If there is a segment in the encryption field, are you "bronze" or "King"?
随机推荐
[stm32f103rct6] can communication
Physical experiment simulation
JS written test question -- prototype, new, this comprehensive question
Banana pie bpi-m5 toss record (2) -- compile u-boot
[Kali's sshd service is enabled]
Canvas record
Mgre.hdlc.ppp.chap.nat comprehensive experiment
How to take the mold for the picture of 1.54 inch TFT st7789 LCD screen
# CF #808 Div.2(A - C)
Dc-1-practice
"Introduction to interface testing" punch in day08: can you save all parameters to excel for test data?
Selenium framework operation steelth.min.js file hides browser fingerprint features
Experiment 4 CTF practice
mysql_ User table_ Field meaning
DOM node type
Dynamic planning of force buckle punch in summary
Reasons for not sending requests after uni app packaging
Map set learning
Use unicloud cloud function to decode wechat motion steps in applet
Conceptual distinction between Po, Bo, VO, dto and POJO