mirror of
https://github.com/heqin-zhu/algorithm.git
synced 2024-03-22 13:30:46 +08:00
Update permute and add dft.py
This commit is contained in:
parent
e20afa6485
commit
36ebaecf18
|
@ -74,8 +74,8 @@ Some pictures and ideas are from `<<Introduction to Algotithm>>`
|
|||
* [isBipartGraph.py](./graph/isBipartGraph.py)
|
||||
* [math](./math)
|
||||
* [README.md](./math/README.md)
|
||||
* [dft.py](./math/dft.py)
|
||||
* [fastPow.py](./math/fastPow.py)
|
||||
* [fft.py](./math/fft.py)
|
||||
* [fibonacci](./math/fibonacci)
|
||||
* [numWeight](./math/numWeight)
|
||||
* [numberTheory](./math/numberTheory)
|
||||
|
|
|
@ -9,15 +9,19 @@
|
|||
# Description:
|
||||
#########################################################################
|
||||
'''
|
||||
def permute(n):
|
||||
def _util(lst,i):
|
||||
if i==n:print(lst)
|
||||
else:
|
||||
for j in range(i,n):
|
||||
lst[i],lst[j]=lst[j],lst[i]
|
||||
_util(lst,i+1)
|
||||
lst[i],lst[j]=lst[j],lst[i]
|
||||
_util([i for i in range(n)],0)
|
||||
|
||||
if __name__=='__main__':
|
||||
|
||||
def permute(n):
|
||||
def _util(lst, i):
|
||||
if i == n:
|
||||
print(lst)
|
||||
else:
|
||||
for j in range(i, n):
|
||||
lst[i], lst[j] = lst[j], lst[i]
|
||||
_util(lst, i+1)
|
||||
lst[i], lst[j] = lst[j], lst[i]
|
||||
_util([i for i in range(n)], 0)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
permute(5)
|
||||
|
|
|
@ -9,15 +9,19 @@
|
|||
# Description:
|
||||
#########################################################################
|
||||
'''
|
||||
def permute(lst,n):
|
||||
|
||||
|
||||
def permute(lst, n):
|
||||
''' O(n!), optimal'''
|
||||
if n==1:print(lst)
|
||||
if n == 1:
|
||||
print(lst)
|
||||
else:
|
||||
for i in range(n):
|
||||
lst[i],lst[n-1] = lst[n-1],lst[i]
|
||||
permute(lst,n-1)
|
||||
lst[i],lst[n-1] = lst[n-1],lst[i]
|
||||
lst[i], lst[n-1] = lst[n-1], lst[i]
|
||||
permute(lst, n-1)
|
||||
lst[i], lst[n-1] = lst[n-1], lst[i]
|
||||
|
||||
if __name__=='__main__':
|
||||
|
||||
if __name__ == '__main__':
|
||||
n = 3
|
||||
permute([i for i in range(n)],n)
|
||||
permute([i for i in range(n)], n)
|
||||
|
|
Loading…
Reference in New Issue
Block a user