当前位置:网站首页>用一个栈实现另一个栈的排序
用一个栈实现另一个栈的排序
2022-07-25 07:38:00 【dlz456】
题目:一个栈中元素的类型为整数,现在想将该栈从顶到底按从大到小的顺序排序,只允许申请一个栈。除此之外,可以申请新的变量,但不能申请额外的数据结构。如何完成排序?
解答:将要排序的栈记为stack,另一个栈为help,将stack的栈顶元素弹出,和help栈顶元素比较,stack的栈顶元素小于等于help栈顶元素就把stack的栈顶元素放到heip栈中去,否则就把help的元素一个个的弹出来,一个个的放到stack栈中。重复上述步骤,直到stack栈空为止。经过了上述操作,此时help栈中的元素顺序就是从顶到底按小到大排序。最后只需要把help栈中的元素一个个的放到stack栈中就可以了。
代码:
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());}}
边栏推荐
- 从ACL 2022 Onsite经历看NLP热点
- Before Oracle 19C migration, how important is it to do a good job of rat playback test?
- 【软件测试】包装简历从这几点出发、提升通过率
- [notes for question brushing] search the insertion position (flexible use of dichotomy)
- Growth path - InfoQ video experience notes [easy to understand]
- How does uxdb extract hours, minutes and seconds from date values?
- Tunnel broadcasting and wireless trunking communication broadcasting system - the case of Tiantaishan tunnel
- 线代(矩阵‘)
- Day by day, month by month | Shenzhen potential technology released the extreme accelerated version of molecular docking engine uni docking
- 【Unity入门计划】界面介绍(1)-Scene视图
猜你喜欢

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

Matlab self programming series (1) -- angular distribution function

纳米数据足球数据,足球赛事比分,体育数据api,卡塔尔世界杯
![[dynamic programming] - Knapsack model](/img/0d/c467e70457495f130ec217660cbea7.png)
[dynamic programming] - Knapsack model

【论文笔记】EFFICIENT CNN ARCHITECTURE DESIGN GUIDED BY VISUALIZATION
![[paper notes] effective CNN architecture design guided by visualization](/img/aa/aeeac3f970eac7f110987c523602c8.png)
[paper notes] effective CNN architecture design guided by visualization

Have you got the advanced usage of pytest?

【论文笔记】Next-ViT: Next Generation Vision Transformer for Efficient Deployment in Realistic Industrial

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

【Unity入门计划】界面介绍(1)-Scene视图
随机推荐
When providing digital talent services, Xi Zhi quickly opened its own digital school for each organization
P1046 [NOIP2005 普及组 T1] 陶陶摘苹果
[programmer 2 Civil Servant] summary of some common problems about system research
toolbar的使用
J1 common DOS commands (P25)
[unity introduction program] basic concepts GameObject & components
3. Promise
P1086 [NOIP2004 普及组第二题] 花生采摘
Growth path - InfoQ video experience notes [easy to understand]
【Unity入门计划】基本概念-预制件 Prefab
New version 8.6 SEO quick release system (can be built at source level)
[programmer 2 Civil Servant] I. Basic Knowledge
Offline base tile, which can be used for cesium loading
轮询、中断、DMA和通道
New functions of shixizhi are online. These new functions are online in June. Can you use them?
Use cyclegan to train self-made data sets, popular tutorials, and get started quickly
Elasticserach里delete_by_query的机制是什么?
【论文笔记】Next-ViT: Next Generation Vision Transformer for Efficient Deployment in Realistic Industrial
Introduction to cesium
【Unity入门计划】制作我的第一个小游戏