编程基础
#并查集 def find(index): if parent[index]!=index: parent[index] = find(parent[index]) return parent[index] def uion(index1,index2): parent[find(index1)] = find(index2)
所有可能的排列 s = "1234" ret = [] s = list(s) def perm(pos,s): if pos+1 == len(s): ret.append(''.join(s)) return for i in range(pos,len(s)): s[i],s[pos]=s[pos],s[i] perm(pos+1,s) s[i],s[pos]=s[pos],s[i] perm(0,s) ret
#素数筛/埃氏筛 import math INT_MAX = 1000 dp = [True]*(INT_MAX+1) for i in range(2,math.ceil(math.sqrt(INT_MAX))): k = i while i*k<=INT_MAX: dp[i*k]=False k = k+1 for ind,num in enumerate(dp): if num == True: print(ind)
合并链表;
反转链表