当前位置:网站首页>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());
}
}
}
}
边栏推荐
- Method of adding kernel in Jupiter notebook
- Beginners must see the markdown User Guide
- Tp5.1 login configuration method of whether to login public functions (complete login case)
- Is redis'module'not an internal or external command?
- [stm32f103rct6] motor PWM drive module idea and code
- Tp5.0 background admin access
- IO (1) -io layering
- Daily three questions 7.15
- Time formatting
- Keras load history H5 format model error: attributeerror 'STR' object has no attribute 'decode‘
猜你喜欢

Dynamic planning of force buckle punch in summary

Learning record 12
![[Kali's sshd service is enabled]](/img/1b/180534d51049177254e30c4b783eba.png)
[Kali's sshd service is enabled]

Riotboard development board series notes (VII) -- the use of framebuffer

Domestic edge computing organization and product research

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

Flume's study notes

If there is a segment in the encryption field, are you "bronze" or "King"?

@Retryable @backoff @recover retry the use of annotations

Application method and practical case of sqlmap of penetration test SQL injection
随机推荐
[jailhouse article] scheduling policies and system software architectures for mixed criticality
Go multiplexing
Matlab for circular pit
What should I do when the interface encounters jsonstring
How to take the mold for the picture of 1.54 inch TFT st7789 LCD screen
【C】 Advanced knowledge of file operation
Pypi counts the number of Downloads
Tp5.0 background admin access
Inheritance (prototype)
Sum of "n" numbers of force deduction summary
Daily three questions 7.15
Edit mathematical formulas in markdown
Innobackupex parameter description
mysql_ Backup restore_ Specify table_ Backup table_ Restore table_ innobackup
Backtracking to solve subset problem
Uni app configuration
Mgre.hdlc.ppp.chap.nat comprehensive experiment
Operator explanation - C language
Use unicloud cloud function to decode wechat motion steps in applet
Banana pie bpi-m5 toss record (3) -- compile BSP