当前位置:网站首页>仅用递归函数和栈操作逆序一个栈
仅用递归函数和栈操作逆序一个栈
2022-06-28 13:46:00 【华为云】
仅用递归函数和栈操作逆序一个栈
【题目】
一个栈依次压入1、2、3、4、5,那么从栈顶到栈底分别为5、4、3、2、1。将这个栈转置后,从栈顶到栈底为 1、2、3、4、5,也就是实现栈中元素的逆序,但是只能用递归函数来实现,不能用其他数据结构。
【思路】
要求只能用递归函数实现,需要设计两个函数,函数一实现返回栈底元素并移除;函数二实现逆序栈,需要使用函数一。
说明:图片来自左神的程序代码面试指南,仅供学习使用。
函数一:
//返回栈底元素并移除public static int getAndRemoveLastElement(Stack<Integer> stack){ int res = stack.pop(); if(stack.isEmpty()){ return res; }else{ int last = getAndRemoveLastElement(stack); stack.push(res); return last; }}
函数二:
//逆序栈public static void reverse(Stack<Integer> stack){ if(stack.isEmpty()){ return ; } int i = getAndRemoveLastElement(stack); reverse(stack); stack.push(i);}
【代码】
package keafmd.accumulate.codeinterviewguide.reversestack;import java.util.Stack;/** * Keafmd * * @ClassName: ReverseStack * @Description: 使用函数逆序栈 * @author: 牛哄哄的柯南 * @date: 2022-06-22 11:01 */public class ReverseStack { //返回栈底元素并移除 public static int getAndRemoveLastElement(Stack<Integer> stack){ int res = stack.pop(); if(stack.isEmpty()){ return res; }else{ int last = getAndRemoveLastElement(stack); stack.push(res); return last; } } //逆序栈 public static void reverse(Stack<Integer> stack){ if(stack.isEmpty()){ return ; } int i = getAndRemoveLastElement(stack); reverse(stack); stack.push(i); } public static void main(String[] args) { Stack<Integer> stack = new Stack<>(); for(int i=1;i<=5;i++){ stack.push(i); } System.out.print("逆序前:"); while (!stack.isEmpty()){ System.out.print(stack.pop()+" "); } System.out.println(); for(int i=1;i<=5;i++){ stack.push(i); } reverse(stack); System.out.print("逆序后:"); while (!stack.isEmpty()){ System.out.print(stack.pop()+" "); } System.out.println(); }}效果:
逆序前:5 4 3 2 1 逆序后:1 2 3 4 5 Process finished with exit code 0边栏推荐
- G : 最大流问题
- Mobile web training -flex layout test question 1
- 华泰证券开户怎么开 怎么办理开户最安全
- PCB懂王,你是吗?我不是
- 公司领导说,个人代码超10个Bug就开除,是什么体验?
- 程序员坐牢了,会被安排去写代码吗?
- Latest summary! 30 provinces announce 2022 college entrance examination scores
- php获取数字的个位数并替换为指定的尾数
- To be the Italian Islander? Liuqiangdong cashed out 6.6 billion yuan in two months and made a one-time 560million "emergency transfer" to buy the European maritime Palace
- PostgreSQL surpasses MySQL
猜你喜欢
![(original) [Maui] realize](/img/76/d79b9cf4ed44870bb20a189315def9.jpg)
(original) [Maui] realize "floating action button" step by step

Oracle 云基础设施扩展分布式云服务,为组织提供更高的灵活性和可控性

China Radio and television 5g package is coming, lower than the three major operators, but not as low as expected

单元测试 CI/CD

腾讯云国际云服务器登录之后没有网络,如何排查?

PCB懂王,你是吗?我不是

How to solve the problem that the computer wireless network does not display the network list

开源社邀您参加OpenInfra Days China 2022,议题征集进行中~

Mobile web training day-2

程序员坐牢了,会被安排去写代码吗?
随机推荐
How to solve the problem that the computer wireless network does not display the network list
Operation and maintenance thinking | do you know the relationship between CMDB and monitoring?
Prediction of red wine quality by decision tree
2022年中国运维安全产品市场规模及发展趋势预测分析
Votre Code parle? (1)
Pytorch model
Play NAS home NAS server setup scheme "suggestions collection"
Align content attribute in flex layout
初识exception
China Database Technology Conference (DTCC) specially invited experts from Kelan sundb database to share
Jupyter notebook中添加虚拟环境
How to open an account on the flush? Is it safe
ThreadLocal的简单理解
Inftnews | technology giants accelerate their march into Web3 and metauniverse
thinkphp6 多级控制器目录访问解决方法
30 sets of JSP website source code collection "suggestions collection"
Other domestic mobile phones failed to fill the vacancy of Huawei, and apple has no rival in the high-end mobile phone market
Ruijie switch configuration SSH password login command [easy to understand]
Build a learning environment
MySQL多表联合查询