java-如何使用推和弹出操作的混合顺序打印所有可能的顺序

假设在LIFO堆栈上执行了压入和弹出操作的混合序列,如何打印所有可能的序列?我可以判断这与递归有关.例如,如果给出订单1 2 3,则输出为

1 2 3
1 3 2
2 1 3
2 3 1
3 2 1

解决方法:

使用Google GUAVA的方法https://google.github.io/guava/releases/19.0/api/docs/com/google/common/collect/Collections2.html#orderedPermutations(java.lang.Iterable)获取所有可能的排列,然后对每个排列使用https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#reverse(java.util.List)颠倒顺序

上一篇:codeforces Beautiful Numbers


下一篇:python-在Pandas的3列的笛卡尔乘积后组织行