随笔分类 - 算法python实现
摘要:#encoding=utf-8 def recersal(content): x=0 y=[] n3=[] word_content=re.findall(r"\w*[!.,?]*",content) n1=[i for i in range(len(word_content)) if word_c
阅读全文
摘要:#encoding=utf-8import timetime_start=time.time()print u"列表去重的七种方法"print u"第一种测试方法"repeat_list=[1,2,4,1,5,1,2,5]result=[]for i in repeat_list: if i not
阅读全文
摘要:#encode=utf-8 import time print u"写一个方法判断一个数是否能被2整除" def whole1(x): try: n=0 x=int(x) if x<2: print u"{0}不是2".format(x) while x>1: if x%2!=0: print u"
阅读全文
摘要:import re def check_ip4_1(ip): result=[] if ip.count(".")==3: for i in ip.split("."): try: if i.strip().startswith("0"): result.append(1) elif int(i.s
阅读全文
摘要:插入排序算法:def insertSort(listx): n=len(listx) for i in range(1,n): key=listx[i] j=i-1 while j>0: if listx[j]>key: listx[j+1]=listx[j] listx[j]=key j-=1 e
阅读全文
摘要:def fastSort1(x,s,e): i=-1 flog=x[e] for j in range(s,e-1): if x[i]>flog: x[i+1],x[j]=x[j],x[i+1] i+=1 x[i+1],x[e]=x[e],x[i+1] return i+1 def fastSort
阅读全文
摘要:x="123456789" y=[1,2,3,4,5,6,7,8,9] n=len(x) m=len(y) #one for i in range(m-1,-1,-1): print y[i] for i in range(n-1,-1,-1): print x[i] #two y.reverse(
阅读全文
摘要:#encoding=utf-8 class TreeNode(object): def __init__(self,var,left=None,right=None): self.var=var self.left=left self.right=right class PreOder(object
阅读全文
摘要:完全二叉树先序遍历 完全二叉树概念: 1、没有叶子节点得都在最后两层2、且最后一层得节点都集中在左边,倒数第二层没有空节点 先序遍历 先遍根节点,再遍历左子树,再到右子树 中序遍历 先遍历左子数,再遍历根节点再遍历右子树 后序遍历 先遍历左子树,再遍历右子树,再遍历根节点 先序遍历 先遍根节点,再遍
阅读全文
摘要:#encoding=utf-8 from Queue import Queue class TreeNode(object): def __init__(self,var,left=None,right=None): self.var=var self.left=left self.right=ri
阅读全文
摘要:def MaxHeap(heap,heapsize,i):#获取最大值堆核心算法 left=2*i+1 right=2*i+2 sign=i if left: sign=left if right: sign=right if i!=sign: heap[i],heap[sign]=heap[sig
阅读全文
摘要:反转加排序: a=["123","456","231"] 第一种办法 sorted(map(lambda x:"".join([i for i in (lambda x:reversed(list(x)))(x)]),a)) 第二种办法 sorted(map(lambda x:x[::-1],a))
阅读全文
摘要:class SortPicture(object): def __init__(self): self.grap={} self.visited={} def addnode(self,node): if node not in self.grap: self.grap[node]=[] def a
阅读全文

浙公网安备 33010602011771号