wzr1005 / wzr1005.github.io

0 stars 0 forks source link

输出栈序列,栈的递归各种脑洞 | Light of the Seven's blog #4

Open wzr1005 opened 5 years ago

wzr1005 commented 5 years ago

https://wzr1005.github.io/2019/03/08/%E8%BE%93%E5%87%BA%E6%A0%88%E5%BA%8F%E5%88%97-%E6%A0%88%E7%9A%84%E9%80%92%E5%BD%92%E5%90%84%E7%A7%8D%E8%84%91%E6%B4%9E/

一个人在群里问了一个好像很基础的题:从1到n压入栈,如何写出打印所有合法出栈序列的code。 当时楞了一下,联想到入栈是先序序列,是不是递归求中序列?不对, 是不是跟全排列有关?好像多此一举,另外全排列的解法不见得会,我估计很多人都不会,这算很基础的题,但可不一般的难/。/ 琢磨了几个小时,写出了这种解法。 #include <cstdio> #include <algorith