当前位置:网站首页>用一个栈实现另一个栈的排序
用一个栈实现另一个栈的排序
2022-06-28 03:33:00 【牛哄哄的柯南】
用一个栈实现另一个栈的排序
【题目】
一个栈中元素的类型为整型,现在想将该栈从顶到底按从大到小的顺序排序,只许申请一个栈。除此之外,可以申请新的变量,但不能申请额外的数据结构。如何完成排序?
【思路】
需要排序的栈:stackData,用一个辅助栈:stackHelp
从stackData开始pop,记录为cur,那cur和stackHelp的栈顶比较,如果stackHelp的栈顶比cur小,就把stackHelp栈的数据一直往stackData压,知道条件不成立(stackHelp.peek()<cur),前提肯定是stackHelp不为空,然后就把cur压入stackHelp中,模拟下就可以看到一个现象,stackHelp的栈底总是比较大,然后往上面变小,最后stackHelp变成了从栈顶到栈底,数据逐渐变大,最后把stackHelp的数据弹出压入stackData,就行了,就实现了stackData栈从顶到底按从大到小。
【代码】
package pers.keafmd.accumulate.codeinterviewguide边栏推荐
- 数组的方法
- 多线程与高并发五:等待队列及Executor和线程池详解(重点)
- 谈云原生,不得不谈的容器
- 门级建模—学习笔记
- 光伏板怎么申请ASTM E108阻燃测试?
- 以自动化赋能转型,飞鹤乳业加速迈向数字化!
- Unity C # e-learning (11) -- custom protocol generation tool
- Chapter 14 AC-DC power supply front stage circuit note I
- Principle and Simulation of switching power supply buck circuit
- How to write anti shake throttling for small programs?
猜你喜欢

Arrangement of basic electrical knowledge (I)

开关电源—Buck电路原理及其仿真

谈云原生,不得不谈的容器

从遇见大咖到成为大咖,昇腾AI开发者创享日给开发者带来无限可能

多项目设计开发·类库项目引入入门

Notes to friendship chain

机器人编程教育的市场竞争力

How to write a software test report? Here comes the third party performance report template

数字电路学习笔记(二)

ambari SSLError: Failed to connect. Please check openssl library versions.
随机推荐
A Preliminary Study of Blackbody radiation
使用信号分析器
English grammar_ Adjective / adverb Level 3 - Comparative_ Useful Expressions
A preliminary study of blackbody radiation
黑體輻射初探
GCD maximum common divisor
How the uni app automatically switches the requested address according to the environment
Does the applet image component not display pictures?
光伏板怎么申请ASTM E108阻燃测试?
vscode中出现无法在只读编辑器中编辑
用Pycharm开发Flask框架设置debug模式没有效果的解决办法
门级建模—学习笔记
Adder - Notes
leetcode - 329. 矩阵中的最长递增路径
Pychart shares third-party modules among different projects
One article tells you what kubernetes is
leetcode:494. All methods of adding and subtracting operators to the array to get the specified value
Particle theory of light (photoelectric effect / Compton effect)
一千行 MySQL 学习笔记,值得收藏!
小程序的防抖节流怎么写?