Skip to content

Instantly share code, notes, and snippets.

@creamidea
Created December 26, 2013 12:30
Show Gist options
  • Save creamidea/8133272 to your computer and use it in GitHub Desktop.
Save creamidea/8133272 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 字典法
global counter
counter = 0
def gem(a, k):
"""
k是用来表明这次递归从那个固定的数开始
"""
n = len(a)
global counter
if k is n:
print_result(a)
else:
for i in range(k, n):
counter = counter+1
a[i], a[k] = a[k], a[i]
gem(a, k+1)
a[k], a[i] = a[i], a[k]
def print_result(a):
print [i for i in a]
if __name__ == '__main__':
gem([1,2,3], 0)
print counter
@creamidea
Copy link
Author

字典法生成全排列图解。
全排列

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment