6
我想知道next_permutation函數的時間複雜度。我可以查看它的代碼嗎?C++中std :: next_permutation()函數的時間複雜度是多少?
我想知道next_permutation函數的時間複雜度。我可以查看它的代碼嗎?C++中std :: next_permutation()函數的時間複雜度是多少?
見http://www.sgi.com/tech/stl/next_permutation.html:
線性。最多(最後 - 第一)/ 2 掉期。
要查看源代碼,只需查看系統的STL頭文件即可。在類似Unix的系統上,您可能需要尋找類似/usr/include/c++/4.1.2/bits/stl_algo.h
的地方。
實際上,它比線性好。這是攤銷O(1) - 即,如果你叫它n!次,它只需要O(n!)操作,而不是n * n !.見[這個問題](http://stackoverflow.com/questions/4973077/the-amortized-complexity-of-stdnext-permutation)。 – ShreevatsaR 2011-06-30 11:58:30