python非递归全排列
刚刚开始学习python,按照廖雪峰的网站看的,当前看到了函数这一节。结合数组操作,写了个非递归的全排列生成。原理是插入法,也就是在一个有n个元素的已有排列中,后加入的元素,依次在前,中,后的每一个位置插入,生成n+1个新的全排列。因为Python切割数组或者字符串,以及合并比较方便,所以,程序会节省很多代码。
def getArrayInsertCharToStr(STR,CHAR):
arr =[]
s_len = len(STR)
index =0
while index <= s_len:
#分割字符串
arr.append(STR[:index]+CHAR+STR[index:s_len])
index = index + 1
return arr def getArrayInsertCharToArray(array,CHAR):
index = 0
re_array = []
while index < len(array):
re_array = re_array + getArrayInsertCharToStr(array[index],CHAR)
index = index + 1
return re_array def getPermutation(STR):
resultArr = [STR[0]]
for item in STR[1:]:
resultArr = getArrayInsertCharToArray(resultArr,item)
return resultArr print(getPermutation('abc'))
python非递归全排列的更多相关文章
- Python 非递归遍历图
class Queue: def __init__(self,max_size): self.max_size = int(max_size) self.queue = [] def put(self ...
- 非递归全排列 python实现
python algorithm 全排列(Permutation) 排列(英语:Permutation)是将相异物件或符号根据确定的顺序重排.每个顺序都称作一个排列.例如,从一到六的数字有720种排列 ...
- Python非递归实现二叉树的后续遍历
leetcode 145. Binary Tree Postorder Traversal 思路一: 使用一个栈stack保存经过的根结点,另一个栈flag保存每个结点的右子树是否遍历: 如果根结点存 ...
- Python非递归遍历多叉树
class Queue: def __init__(self,max_size): self.max_size = int(max_size) self.queue = [] def put(self ...
- 全排列问题(递归&非递归&STL函数)
问题描述: 打印输出1-9的所有全排序列,或者打印输出a-d的全排列. 思路分析: 将每个元素放到余下n-1个元素组成的队列最前方,然后对剩余元素进行全排列,依次递归下去. 比如:1 2 3 为例首先 ...
- 【python中二叉树的实现】python中二叉树的创建、三种方式递归遍历和非递归遍历
代码如下: # coding=utf-8 class myNode(object): def __init__(self, data=-1, lchild=None, rchild=None): se ...
- python实现图的遍历(递归和非递归)
class graph: def __init__(self,value): self.value=value self.neighbors=None # 图的广度优先遍历 # 1.利用队列实现 # ...
- 递归/非递归----python深度遍历二叉树(前序遍历,中序遍历,后序遍历)
递归代码:递归实现很简单 '二叉树结点类' class TreeNode: def __init__(self, x): self.val = x self.left = None self.righ ...
- python递归与非递归实现斐波那契数列
1.题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). 递归实现: class Solution(): def Fibnacci(self ...
随机推荐
- 阻止网页内部滚动条mousewheel事件冒泡
function preventScroll(id){ var _this = document.getElementById(id); if(navigator.userAgent.indexOf( ...
- kibana 使用
统计IP前五的数据 统计相应时间 状态码统计 统计IP地图
- magento目录结构说明,Magento文件夹结构说明,Magento folder structure
/app – 程序根目录 /app/etc – 全局配置文件目录 /app/code – 所有模块安装其模型和控制器的目录 /app/code/core – 核心代码或经过认证 ...
- iOS真机运行 Xcode报错(libpng error: CgBI: unhandled critical chunk)问题已解决;
Cocos2d-x加载图片资源出现libpng error: CgBI: unhandled critical chunk Xcode7.3 设置Remove Text Metadata From P ...
- with CTE AS
CTE/表变量/Temp http://www.cnblogs.com/ziyeyimeng/articles/2366855.html
- TIJ——Chapter Eight:Polymorphism
The twist |_Method-call binding Connecting a method call to a method body is called binding. When bi ...
- struts-OGNL
特点 常用来访问值栈里对象属性的一种语言 通常由struts标签来解析执行 <%@ taglib prefix="s" uri="/struts-tags" ...
- html5,增加flash插件
<embed src="2.swf" type="" width="500" height="" >< ...
- httpclient4 文档翻译
前言超文本传输协议(HTTP)也许是当今互联网上使用的最重要的协议了.Web服务,有网络功能的设备和网络计算的发展,都持续扩展了HTTP协议的角色,超越了用户使用的Web浏览器范畴,同时,也增加了需要 ...
- Oracle基本sql操作
1.查询用户下的所有表 查询用户下的所有表 select distinct table_name from user_tab_columns; 2.搜索出前N条记录 Select a.*,rownum ...