当前位置:网站首页>Using one stack to sort another stack
Using one stack to sort another stack
2022-07-25 07:39:00 【dlz456】
subject : The type of elements in a stack is integer , Now I want to sort the stack from top to bottom in the order of big to small , Only one stack is allowed . besides , You can apply for new variables , But you can't apply for additional data structures . How to sort ?
answer : Mark the stack to be sorted as stack, The other stack is help, take stack The top element of the stack pops up , and help Stack top element comparison ,stack The top element of the stack is less than or equal to help The top element of the stack is stack Put the top element of the stack into heip Go in the stack , Otherwise, I will help The elements of spring out one by one , Put them one by one stack In the stack . Repeat the above steps , until stack Stack empty until . After the above operation , here help The order of elements in the stack is from top to bottom, from small to large . Finally, just put help Put the elements in the stack one by one stack Just in the stack .
Code :
public void sort(){
public Stack<int> help=new Stack<int>();
while(!stack.empty()){
int cur=stack.pop();
while(!help.empty()&&cur>help.peek(){
stack.push(help.pop());}
help.push(cur);}
while(!help.empty()){
stack.push(help.pop());}}
边栏推荐
- Analysis of common classes of Servlet
- P1047 [NOIP2005 普及组 T2] 校门外的树
- Elasticserach里delete_by_query的机制是什么?
- 如何仅用递归函数和栈操作逆序一个栈
- How should enterprise users choose aiops or APM?
- MathWorks has been in China for 15 years. What are the secrets of continuous innovation?
- Room database migration
- Quickly build a centralized logging platform through elk
- Polling, interrupt, DMA and channel
- [unity entry program] make my first little game
猜你喜欢

Huawei wireless device configuration wpa2-802.1x-aes security policy

【Unity入门计划】界面介绍(1)-Scene视图
![[unity entry plan] interface Introduction (1) -scene view](/img/88/dee292cb90cd740640018e7260107f.png)
[unity entry plan] interface Introduction (1) -scene view

How to use network installation to deploy multiple virtual servers in KVM environment

【Unity入门计划】基本概念-2D刚体Rigidbody 2D

Robot framework mobile terminal Automation Test ----- 01 environment installation

Beijing internal promotion | Microsoft STCA recruits nlp/ir/dl research interns (remote)

Configuring WAPI certificate security policy for Huawei wireless devices

A fast method of data set enhancement for deep learning

Nano data, football data, football match scores, sports data API, Qatar world cup
随机推荐
[unity introduction program] basic concepts -2d rigid body 2D
When deep learning makes data sets, it specifies how many frames to extract an image from the long video to the specified file path
Pads export Gerber file
Native form submission data
Quickly build a centralized logging platform through elk
【PyTorch】最常见的view的作用
Analysis of common classes of Servlet
QT学习日记20——飞机大战项目
webflux默认io线程数
Learn when playing No 7 | don't study this holiday, study only
Tunnel broadcasting and wireless trunking communication broadcasting system - the case of Tiantaishan tunnel
轮询、中断、DMA和通道
QT learning diary 20 - aircraft war project
整数a按位取反(~)后的值为-(a+1)
[programmer 2 Civil Servant] IV. common problems
【Unity入门计划】基本概念-2D刚体Rigidbody 2D
冰冰学习笔记:类与对象(上)
Gather the wisdom of developers and consolidate the foundation of the database industry
Robot Framework移动端自动化测试----01环境安装
How to use network installation to deploy multiple virtual servers in KVM environment