算法问题:查找从堆栈中弹出的所有序列
Algorithm problem: find all sequences popped from the stack
假设一个[1, 2, 3, 4]
列表要推送到堆栈,从堆栈弹出的可能顺序是:
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 3 1
3 2 1 4
3 2 4 1
3 4 2 1
4 3 2 1
我找到了一个C++版本的算法,它给出了正确的输出:
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <stdlib.h>
#include <cctype>
#include <stack>
#include <queue>
using namespace std;
void printAllOutStackSeq( queue<int> inQueue, int n, stack<int> st, queue<int> out )
{
if( n <= 0 || ( inQueue.empty() && st.empty() && out.empty() ) )
{
return;
}
if( out.size() == n )
{
while( !out.empty() )
{
cout << out.front() << ' ';
out.pop();
}
cout << endl;
return;
}
stack<int> stCopy = st;
queue<int> outCopy = out;
if( !st.empty() )
{
out.push( st.top() );
st.pop();
printAllOutStackSeq( inQueue, n, st, out );
}
if( !inQueue.empty() )
{
stCopy.push( inQueue.front() );
inQueue.pop();
printAllOutStackSeq( inQueue, n, stCopy, outCopy );
}
return;
}
int main()
{
int ret = 0;
int a[] = { 1, 2, 3, 4 };
queue<int> inQueue;
for( int i = 0; i < 4; i++ )
{
inQueue.push( a[i] );
}
int n;
stack<int> st;
queue<int> out;
printAllOutStackSeq( inQueue, 4, st, out );
return ret;
}
在我将C++转换为Python后,如下所示:
from typing import List
import copy
def print_out_stack_seq(to_push: List[int], n,
stack: List[int],
popped: List[int]):
if n <= 0 or (not to_push and not stack and not popped):
return
if len(popped) == n:
while len(popped):
print(popped.pop(0), end=' ')
print()
return
stack_copy = copy.copy(stack)
popped_copy = copy.copy(popped)
if stack:
popped.append(stack.pop())
print_out_stack_seq(to_push, n, stack, popped)
if to_push:
stack_copy.append(to_push.pop(0))
print_out_stack_seq(to_push, n, stack_copy, popped_copy)
return
to_push = [1, 2, 3, 4]
n = len(to_push)
stack = []
popped = []
print_out_stack_seq(to_push, n, stack, popped)
它只打印第一行:
1 2 3 4
我真的不知道为什么,有人能帮我吗?
在C++版本中,inQueue是通过递归调用中的副本传递的。在您的python版本中,inQueue不是to_push。
在调用print_out_stack_seq之前添加to_push的副本似乎可以解决您的问题。
from typing import List
import copy
def print_out_stack_seq(to_push: List[int], n,
stack: List[int],
popped: List[int]):
if n <= 0 or (not to_push and not stack and not popped):
return
if len(popped) == n:
while len(popped):
print(popped.pop(0), end=' ')
print()
return
stack_copy = copy.copy(stack)
popped_copy = copy.copy(popped)
if stack:
popped.append(stack.pop())
print_out_stack_seq(copy.copy(to_push), n, stack, popped)
if to_push:
stack_copy.append(to_push.pop(0))
print_out_stack_seq(copy.copy(to_push), n, stack_copy, popped_copy)
return
to_push = [1, 2, 3, 4]
n = len(to_push)
stack = []
popped = []
print_out_stack_seq(to_push, n, stack, popped)
输出:
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 3 1
3 2 1 4
3 2 4 1
3 4 2 1
4 3 2 1
然而,这个输出似乎不是正确的答案,因为序列(4 3 1 2(是有效的,但没有打印出来。正如其他人提到的,你打印的是所有排列的列表。在python中,可以使用以下代码完成:
import itertools
to_push = [1, 2, 3, 4]
for p in itertools.permutations(to_push):
print(p)
输出:
(1, 2, 3, 4)
(1, 2, 4, 3)
(1, 3, 2, 4)
(1, 3, 4, 2)
(1, 4, 2, 3)
(1, 4, 3, 2)
(2, 1, 3, 4)
(2, 1, 4, 3)
(2, 3, 1, 4)
(2, 3, 4, 1)
(2, 4, 1, 3)
(2, 4, 3, 1)
(3, 1, 2, 4)
(3, 1, 4, 2)
(3, 2, 1, 4)
(3, 2, 4, 1)
(3, 4, 1, 2)
(3, 4, 2, 1)
(4, 1, 2, 3)
(4, 1, 3, 2)
(4, 2, 1, 3)
(4, 2, 3, 1)
(4, 3, 1, 2)
(4, 3, 2, 1)
相关文章:
- 算法问题:查找从堆栈中弹出的所有序列
- 在 for 循环中查找问题时遇到困难
- 介绍类 在C++中查找圆半径的问题
- 查找不等式为真的次数时出现问题
- Distirbute Candy - 查找问题的最小可重现示例
- 查找在 4 和问题中代码失败的测试用例
- 在文本中查找半个单词"ABBA"的问题
- 查找有问题的宏
- 使用用户定义的函数查找数字的幂时出现问题
- 使用二分法查找数的平方根时出现问题
- 如何解决非限定名称查找问题
- 使用STD :: MAP在数据及其性能问题中查找重复项.我可以预先分配吗?
- 迭代列表以查找元素的出现.代码问题
- 在C++的数组中查找最低值时遇到问题
- 查找料箱包装问题中的所有可能变化
- 关于获取行和字符串查找函数的问题
- C++constexpr数组查找:内存开销?其他问题
- 我的递归算法中的问题,用于查找所有最短、唯一的路径
- 在二叉树中插入/查找问题
- 查找第一个文件问题无法使任何示例正常工作。