第一行输入一个N,表示栈中元素的个数第二行输入N个整数表示栈顶到栈底的各个元素
输出一行表示排序后的栈中栈顶到栈底的各个元素。
5 5 8 4 3 6
8 6 5 4 3
1 <= N <= 10000-1000000 <= <= 1000000
import java.util.*; public class Main{ public static void sortStack(Stack<Integer> stack) { Stack<Integer> help = new Stack<>(); while (!stack.isEmpty()) { int cur = stack.pop(); while (!help.isEmpty() && cur > help.peek()) { stack.push(help.pop()); } help.push(cur); } while (!help.isEmpty()) { stack.push(help.pop()); } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] arr = new int[n]; for (int i = n - 1; i >= 0; i--) { arr[i] = scanner.nextInt(); } // for (int i = 0; i < n; i++) { // 错误示范: case通过率为95.24% // arr[i] = scanner.nextInt(); // } Stack<Integer> stack = new Stack(); for (int item : arr) { stack.push(item); } sortStack(stack); while (!stack.isEmpty()) { System.out.print(stack.pop() + " "); } } }
import sys class OrderHelpStack(object): def orderStack(self, stack): helpStack = [] while stack: stackTop = stack.pop() while helpStack and stackTop > helpStack[-1]: stack.append(helpStack.pop()) helpStack.append(stackTop) while helpStack: stack.append(helpStack.pop()) return stack N = int(sys.stdin.readline().strip()) stack = [] orderHelpStack = OrderHelpStack() line = sys.stdin.readline().strip() words = line.split() for word in words: stack.append(int(word)) stack = orderHelpStack.orderStack(stack) result = "" for i in range(N-1): result += str(stack.pop()) + " " result += str(stack.pop()) print(result)
#include<bits/stdc++.h> using namespace std; int main() { int i; int n,x; stack<int> s,help; cin>>n; vector<int> arr(n,0); for(i=0;i<n;i++) { cin>>x; arr.at(i)=x; } for(i=n-1;i>=0;i--) s.push(arr.at(i)); while(!s.empty()) { int temp=s.top(); s.pop(); while(!help.empty()&&help.top()>temp) //这里一开始第二条件使用help.top()>=temp,结果 //超时严重,应该是测试数据有极端测试数据,导致程序 //交换过多导致的 { s.push(help.top()); help.pop(); } help.push(temp); } while(!help.empty()) { if(help.size()==1) cout<<help.top(); else cout<<help.top()<<" "; help.pop(); } return 0; }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Deque; import java.util.LinkedList; public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { br.readLine(); String[] secondLine = br.readLine().split(" "); Deque<Integer> stack = new LinkedList<>(); Deque<Integer> help = new LinkedList<>(); for (String s : secondLine) { stack.push(Integer.parseInt(s)); } while (!stack.isEmpty()) { int cur = stack.pop(); while (!help.isEmpty() && cur > help.peek()) { stack.push(help.pop()); } help.push(cur); } while (!help.isEmpty()) { stack.push(help.pop()); } StringBuilder res = new StringBuilder(); while (!stack.isEmpty()) { res.append(stack.pop()); if (!stack.isEmpty()) { res.append(" "); } } System.out.println(res); } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.Stack; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Stack<Integer> stack1 = new Stack<>(), stack2 = new Stack<>(); int n = Integer.parseInt(br.readLine()); String[] strArr = br.readLine().split(" "); for(int i = 0; i < n; i++) stack1.push(Integer.parseInt(strArr[i])); while(!stack1.isEmpty()){ int peekVal = stack1.pop(); if(stack2.isEmpty()){ stack2.push(peekVal); }else{ if(peekVal >= stack2.peek()){ stack2.push(peekVal); }else{ while(!stack2.isEmpty() && peekVal < stack2.peek()) stack1.push(stack2.pop()); stack2.push(peekVal); } } } while(!stack2.isEmpty()) System.out.print(stack2.pop() + " "); } }
import java.util.Scanner; import java.util.Stack; public class Main { public static void sortStackByStack(Stack<Integer> stack) { Stack<Integer> help = new Stack<>(); while (!stack.isEmpty()) { int temp = stack.pop(); while (!help.isEmpty() && help.peek() > temp) { stack.push(help.pop()); } help.push(temp); } while (!help.isEmpty()) { int temp = help.pop(); stack.push(temp); System.out.print(temp + " "); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Stack<Integer> stack = new Stack<>(); while (n-- > 0) { stack.push(sc.nextInt()); } sortStackByStack(stack); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; public class Main { public static void sortStackByStack(Stack<Integer> stack) { Stack<Integer> help = new Stack<>(); while (!stack.isEmpty()) { int cur = stack.pop(); while (!help.isEmpty() && help.peek() < cur) { stack.push(help.pop()); } help.push(cur); } while (!help.isEmpty()) { stack.push(help.pop()); } } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); Stack<Integer> helpInput = new Stack<>(); int n = Integer.parseInt(in.readLine()); String[] stackArray = in.readLine().split(" "); for (int i = n-1;i >=0;i--){ helpInput.push(Integer.parseInt(stackArray[i])); } sortStackByStack(helpInput); while (!helpInput.isEmpty()){ System.out.print(helpInput.pop()+ " "); } in.close(); } }
#include <bits/stdc++.h> using namespace std; int main(){ stack<int> S1, S2; int n, x, t; cin>>n; for(int i=0;i<n;i++){ cin>>x; S1.push(x); } while(!S1.empty()){ x = S1.top(); S1.pop(); if(S2.empty()) S2.push(x); else{ t = S2.top(); while(!S2.empty() && t>x){ S2.pop(); S1.push(t); t = S2.top(); } S2.push(x); } } while(!S2.empty()){ cout<<S2.top()<<" "; S2.pop(); } return 0; }
package main import ( "fmt" ) func main() { num := 0 fmt.Scan(&num) s := Stack{} for i := 0; i < num; i++ { v := 0 fmt.Scan(&v) s.Push(v) } sortStack(&s) for !s.IsEmpty() { fmt.Print(s.Pop()) fmt.Print(" ") } fmt.Println("") } func sortStack(s *Stack) { t := Stack{} for !s.IsEmpty() { top := s.Pop() for !t.IsEmpty() && top > t.Top() { s.Push(t.Pop()) } t.Push(top) } for !t.IsEmpty() { s.Push(t.Pop()) } } type Stack struct { data []int } func (s *Stack) Push(v int) { s.data = append(s.data, v) } func (s *Stack) Pop() int { top := s.data[len(s.data)-1] s.data = s.data[:len(s.data)-1] return top } func (s *Stack) IsEmpty() bool { return len(s.data) == 0 } func (s *Stack) Top() int { return s.data[len(s.data)-1] }
import sys # 处理输入 N = sys.stdin.readline().strip() stack = [int(i) for i in sys.stdin.readline().strip().split()] # 使用辅助栈,基于汉诺塔原理实现栈的排序 def sortStack(stack): helper = [] while stack: val = stack.pop() if not helper: helper.append(val) else: while helper and val > helper[-1]: stack.append(helper.pop()) helper.append(val) while helper: stack.append(helper.pop()) return stack stack = sortStack(stack) while stack: print(stack.pop(), end=' ')
#include <bits/stdc++.h> using namespace std; stack<int> stOut; // 输出栈 void push(stack<int>& stIn) { while(!stIn.empty()){ int tmp = stIn.top(); stIn.pop(); // 输出栈顶元素比当前元素大,就不断 pop 出来,重新插入输入栈 while(!stOut.empty() && tmp < stOut.top()){ int top = stOut.top(); stOut.pop(); stIn.push(top); } // 否则就插入输出栈 stOut.push(tmp); } } int main() { int n; cin >> n; vector<int> nums(n); for(int i = 0; i < n; ++i) cin >> nums[i]; stack<int> stIn; // 输入栈 for(int i = 0; i < n; ++i){ stIn.push(nums[i]); } push(stIn); while(!stOut.empty()){ cout << stOut.top() << " " ; stOut.pop(); } return 0; }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Stack<Integer> stackA = new Stack<Integer>(); Stack<Integer> stackB = new Stack<Integer>(); while(sc.hasNextInt()){ stackA.push(sc.nextInt()); } while(!stackA.isEmpty()){ int temp=stackA.pop(); while( !stackB.isEmpty() && stackB.peek()>temp){ stackA.push(stackB.pop()); } stackB.push(temp); } while(!stackB.isEmpty()){ System.out.print(stackB.pop()+" "); } } }
# 输入的元素个数 n = int(input()) # 用列表模仿栈 stack = [] number = input().split() number.reverse() for i in range(len(number)): stack.append(int(number[i])) # 辅助的栈 help = [] while len(stack) != 0: a = stack.pop() while len(help) != 0 and help[-1] < a: stack.append(help.pop()) help.append(a) while len(help) != 0: stack.append(help.pop()) # 输出栈顶到栈底 for i in range(len(stack)): print(stack.pop(), end=' ')《程序员代码面试》Python题解 https://zhuanlan.zhihu.com/p/444550262
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.Stack; /** * @author hugh * @create 2021-09-04 23:10 */ public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); String[] line = in.readLine().split(" "); Stack<Integer> data = new Stack<>(); for (int i = n - 1; i >= 0; i--) { data.push(Integer.parseInt(line[i])); } Stack<Integer> stack = new Stack<>(); sort(data, stack); StringBuilder res = new StringBuilder(); while (!data.isEmpty()) { res.append(data.pop() + " "); } System.out.println(res.substring(0, res.length() - 1)); } public static void sort(Stack<Integer> data, Stack<Integer> stack) { while (!data.isEmpty()) { Integer num1 = data.pop(); put(stack, num1); } while (!stack.isEmpty()) { data.push(stack.pop()); } } /** * 将x插入data中的合适位置,保证从大到小的顺序 * @param stack 按照从大到小(栈底->栈顶)排序的栈 * @param x 整数 */ public static void put(Stack<Integer> stack, Integer x) { if (stack.isEmpty() || stack.peek() > x) { stack.push(x); } else { Integer num = stack.pop(); put(stack, x); stack.push(num); } } }
#include<iostream> #include<stack> #include<vector> #include<algorithm> using namespace std; int main() { int N,x; cin>>N; stack<int> stack1,stack2; for(int i=0;i<N;++i){ cin>>x; stack1.push(x); } while(!stack1.empty()){ int cur=stack1.top(); stack1.pop(); while(!stack2.empty() && cur>stack2.top()){ stack1.push(stack2.top()); stack2.pop(); } stack2.push(cur); } while(!stack2.empty()){ stack1.push(stack2.top()); stack2.pop(); } for(int i=0;i<N;++i){ cout<<stack1.top()<<" "; stack1.pop(); } }
十一点吃个🍎 👉🏻map竟然忘记返值
let line=readline() let stack=[],help=[] while(line=readline()){ stack=line.split(' ') stack= stack.map(item=>parseInt(item)) // console.log(stack) while(stack.length){ let cur=stack.pop() if(help.length==0||help[help.length-1]<cur){ help.push(cur) }else{ while(help.length){ var helpcur=help[help.length-1] if(helpcur>cur){ stack.push(help.pop()) }else break } help.push(cur) } } console.log(help.reverse().join(' ')) }
#include <iostream> #include <stack> using namespace std; int main(){ int N=0, val=0; cin >> N; stack<int> st1, st2; while(N--){ //每次插入数据后,都要保证st1的顺序。 cin >> val; while(!st1.empty() && st1.top()>val){ //当前栈不空,并且栈顶元素大于要插入的元素。 st2.push(st1.top()); st1.pop(); } st1.push(val); while(!st2.empty()){ st1.push(st2.top()); st2.pop(); } } while(!st1.empty()){ cout << st1.top() << " "; st1.pop(); } return 0; }
解题思路:
遍历给定的栈,将当前的栈中的元素压入辅助栈中,当压入辅助栈的序列不是递增序列时,就把辅助栈中的元素重新压入原来的栈,直到辅助栈的序列为递归序列。
/* * @Description: 用一个栈实现另一个栈的排序 * @Author: * @Date: 2020-10-27 16:03:00 * @LastEditTime: 2020-10-27 16:27:22 * @LastEditors: Please set LastEditors */ #include<iostream> #include<stack> using namespace std; int main(){ int N, num; cin >> N; stack<int> s1; //表示第一个栈 stack<int> s2; //表示第二个栈 for(int i = 0;i < N;i++){ cin >> num; s1.push(num); } while(!s1.empty()){ s2.push(s1.top()); s1.pop(); } //此时s2中的从栈顶到栈底的元素与输入的元素一致 //下面为用s1将s2进行排序 while(!s2.empty()){ int temp = s2.top(); s2.pop(); while(!s1.empty() && temp < s1.top()){ s2.push(s1.top()); s1.pop(); } s1.push(temp); } //输出s1 while(!s1.empty()){ cout << s1.top() << " "; s1.pop(); } //system("pause"); return 0; }