Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
forked from wangjili/Algorithm
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
The license selected for the repository is subject to the license used by the main branch of the repository.
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
Algorithm
/
src
/
nowCoderClass1
/
section4
/
StackReverse.java
Algorithm
/
src
/
nowCoderClass1
/
section4
/
StackReverse.java
StackReverse.java 2.05 KB
Copy Edit Raw Blame History
RosinaWang authored 2017年09月05日 17:20 +08:00 . first commit
package nowCoderClass1.section4;
import java.util.Stack;
/**
* 实现一个栈的逆序,但是只能用递归函数和这个栈本身的pop操作来实现,而不能自己申请另外的数据结构。
* Created by Dell on 2017年05月07日.
*/
public class StackReverse {
public int[] reverseStack(int[] A, int n) {
// write code here
Stack<Integer> stack=new Stack<Integer>();
//将数组放入栈中
for(int i=0;i<n;i++){
stack.push(A[i]);
}
System.out.print("逆序前"+stack.peek());
//将栈逆序
reverse(stack);
System.out.print("逆序后"+stack.peek());
//将栈中的数放入数组,返回
for(int i=n-1;i>=0;i--){
A[i]=stack.pop();
}
return A;
}
/**
* 使用get方法每次获取最下面的元素,然后将栈里的元素逆序
*/
private void reverse(Stack<Integer> stack) {
int result=get(stack);
if(stack.isEmpty())
stack.push(result);
else{
reverse(stack);
stack.push(result);
}
}
/**
* 将栈反转,使用下列的办法无法将栈反转,只是按原来的顺序压入栈
*/
// private void reverse(Stack<Integer> stack){
// int result=(Integer)stack.pop();
// if(stack.isEmpty()){
// stack.push(result);
// }else{
// reverse(stack);
// stack.push(result);
// }
// }
/**
* get()方法,使用递归将栈底元素拿出,将其他元素依次压入栈中
*/
private int get(Stack stack){
int result=(Integer)stack.pop();//弹出的是当前的栈顶
if(stack.isEmpty()){//到了最底层,返回的就是要
return result;
}else{
int last=get(stack);//一层一层,直到取到栈底,然后将栈底元素一层一层往上传,传的过程中顺便将每一层的元素压栈
stack.push(result);
return last;
}
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

准备数据结构、算法时做过的题 题目包含:基础数据结构,算法,大公司笔试真题,牛客算法题,lintcode,九章算法,《剑指Offer》,左程云《程序员代码面试指南》
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/TonyLee1024/Algorithm.git
git@gitee.com:TonyLee1024/Algorithm.git
TonyLee1024
Algorithm
Algorithm
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /