日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python 编程算法_python语言编程算法

發(fā)布時(shí)間:2024/7/23 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 编程算法_python语言编程算法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

編程題

1 臺(tái)階問題/斐波那契

一只青蛙一次可以跳上1級(jí)臺(tái)階,也可以跳上2級(jí)。求該青蛙跳上一個(gè)n級(jí)的臺(tái)階總共有多少種跳法。

fib = lambda n: n if n <= 2 else fib(n - 1) + fib(n - 2)

第二種記憶方法

def memo(func):

cache = {}

def wrap(*args):

if args not in cache:

cache[args] = func(*args)

return cache[args]

return wrap

@memo

def fib(i):

if i < 2:

return 1

return fib(i-1) + fib(i-2)

第三種方法

def fib(n):

a, b = 0, 1

for _ in xrange(n):

a, b = b, a + b

return b

2 變態(tài)臺(tái)階問題

一只青蛙一次可以跳上1級(jí)臺(tái)階,也可以跳上2級(jí)……它也可以跳上n級(jí)。求該青蛙跳上一個(gè)n級(jí)的臺(tái)階總共有多少種跳法。

fib = lambda n: n if n < 2 else 2 * fib(n - 1)

3 矩形覆蓋

我們可以用2*1的小矩形橫著或者豎著去覆蓋更大的矩形。請(qǐng)問用n個(gè)2*1的小矩形無重疊地覆蓋一個(gè)2*n的大矩形,總共有多少種方法?

第2*n個(gè)矩形的覆蓋方法等于第2*(n-1)加上第2*(n-2)的方法。

f = lambda n: 1 if n < 2 else f(n - 1) + f(n - 2)

4 楊氏矩陣查找

在一個(gè)m行n列二維數(shù)組中,每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請(qǐng)完成一個(gè)函數(shù),輸入這樣的一個(gè)二維數(shù)組和一個(gè)整數(shù),判斷數(shù)組中是否含有該整數(shù)。

使用Step-wise線性搜索。

def get_value(l, r, c):

return l[r][c]

def find(l, x):

m = len(l) - 1

n = len(l[0]) - 1

r = 0

c = n

while c >= 0 and r <= m:

value = get_value(l, r, c)

if value == x:

return True

elif value > x:

c = c - 1

elif value < x:

r = r + 1

return False

5 去除列表中的重復(fù)元素

用集合

list(set(l))

用字典

l1 = ['b','c','d','b','c','a','a']

l2 = {}.fromkeys(l1).keys()

print l2

用字典并保持順序

l1 = ['b','c','d','b','c','a','a']

l2 = list(set(l1))

l2.sort(key=l1.index)

print l2

列表推導(dǎo)式

l1 = ['b','c','d','b','c','a','a']

l2 = []

[l2.append(i) for i in l1 if not i in l2]

sorted排序并且用列表推導(dǎo)式.

l = ['b','c','d','b','c','a','a'] [single.append(i) for i in sorted(l) if i not in single] print single

6 鏈表成對(duì)調(diào)換

1->2->3->4轉(zhuǎn)換成2->1->4->3.

class ListNode:

def __init__(self, x):

self.val = x

self.next = None

class Solution:

# @param a ListNode

# @return a ListNode

def swapPairs(self, head):

if head != None and head.next != None:

next = head.next

head.next = self.swapPairs(next.next)

next.next = head

return next

return head

7 創(chuàng)建字典的方法

1 直接創(chuàng)建

dict = {'name':'earth', 'port':'80'}

2 工廠方法

items=[('name','earth'),('port','80')]

dict2=dict(items)

dict1=dict((['name','earth'],['port','80']))

3 fromkeys()方法

dict1={}.fromkeys(('x','y'),-1)

dict={'x':-1,'y':-1}

dict2={}.fromkeys(('x','y'))

dict2={'x':None, 'y':None}

8 合并兩個(gè)有序列表

知乎遠(yuǎn)程面試要求編程

尾遞歸

def _recursion_merge_sort2(l1, l2, tmp):

if len(l1) == 0 or len(l2) == 0:

tmp.extend(l1)

tmp.extend(l2)

return tmp

else:

if l1[0] < l2[0]:

tmp.append(l1[0])

del l1[0]

else:

tmp.append(l2[0])

del l2[0]

return _recursion_merge_sort2(l1, l2, tmp)

def recursion_merge_sort2(l1, l2):

return _recursion_merge_sort2(l1, l2, [])

循環(huán)算法

思路:

定義一個(gè)新的空列表

比較兩個(gè)列表的首個(gè)元素

小的就插入到新列表里

把已經(jīng)插入新列表的元素從舊列表刪除

直到兩個(gè)舊列表有一個(gè)為空

再把舊列表加到新列表后面

def loop_merge_sort(l1, l2):

tmp = []

while len(l1) > 0 and len(l2) > 0:

if l1[0] < l2[0]:

tmp.append(l1[0])

del l1[0]

else:

tmp.append(l2[0])

del l2[0]

tmp.extend(l1)

tmp.extend(l2)

return tmp

pop彈出

a = [1,2,3,7]

b = [3,4,5]

def merge_sortedlist(a,b):

c = []

while a and b:

if a[0] >= b[0]:

c.append(b.pop(0))

else:

c.append(a.pop(0))

while a:

c.append(a.pop(0))

while b:

c.append(b.pop(0))

return c

print merge_sortedlist(a,b)

9 交叉鏈表求交點(diǎn)

其實(shí)思想可以按照從尾開始比較兩個(gè)鏈表,如果相交,則從尾開始必然一致,只要從尾開始比較,直至不一致的地方即為交叉點(diǎn),如圖所示

# 使用a,b兩個(gè)list來模擬鏈表,可以看出交叉點(diǎn)是 7這個(gè)節(jié)點(diǎn)

a = [1,2,3,7,9,1,5]

b = [4,5,7,9,1,5]

for i in range(1,min(len(a),len(b))):

if i==1 and (a[-1] != b[-1]):

print "No"

break

else:

if a[-i] != b[-i]:

print "交叉節(jié)點(diǎn):",a[-i+1]

break

else:

pass

另外一種比較正規(guī)的方法,構(gòu)造鏈表類

class ListNode:

def __init__(self, x):

self.val = x

self.next = None

def node(l1, l2):

length1, lenth2 = 0, 0

# 求兩個(gè)鏈表長(zhǎng)度

while l1.next:

l1 = l1.next

length1 += 1

while l2.next:

l2 = l2.next

length2 += 1

# 長(zhǎng)的鏈表先走

if length1 > lenth2:

for _ in range(length1 - length2):

l1 = l1.next

else:

for _ in range(length2 - length1):

l2 = l2.next

while l1 and l2:

if l1.next == l2.next:

return l1.next

else:

l1 = l1.next

l2 = l2.next

修改了一下:

#coding:utf-8

class ListNode:

def __init__(self, x):

self.val = x

self.next = None

def node(l1, l2):

length1, length2 = 0, 0

# 求兩個(gè)鏈表長(zhǎng)度

while l1.next:

l1 = l1.next#尾節(jié)點(diǎn)

length1 += 1

while l2.next:

l2 = l2.next#尾節(jié)點(diǎn)

length2 += 1

#如果相交

if l1.next == l2.next:

# 長(zhǎng)的鏈表先走

if length1 > length2:

for _ in range(length1 - length2):

l1 = l1.next

return l1#返回交點(diǎn)

else:

for _ in range(length2 - length1):

l2 = l2.next

return l2#返回交點(diǎn)

# 如果不相交

else:

return

10 二分查找

#coding:utf-8

def binary_search(list, item):

low = 0

high = len(list) - 1

while low <= high:

mid = (high - low) / 2 + low # 避免(high + low) / 2溢出

guess = list[mid]

if guess > item:

high = mid - 1

elif guess < item:

low = mid + 1

else:

return mid

return None

mylist = [1,3,5,7,9]

print binary_search(mylist, 3)

11 快排

#coding:utf-8

def quicksort(list):

if len(list)<2:

return list

else:

midpivot = list[0]

lessbeforemidpivot = [i for i in list[1:] if i<=midpivot]

biggerafterpivot = [i for i in list[1:] if i > midpivot]

finallylist = quicksort(lessbeforemidpivot)+[midpivot]+quicksort(biggerafterpivot)

return finallylist

print quicksort([2,4,6,7,1,2,5])

12 找零問題

#coding:utf-8

#values是硬幣的面值values = [ 25, 21, 10, 5, 1]

#valuesCounts 錢幣對(duì)應(yīng)的種類數(shù)

#money 找出來的總錢數(shù)

#coinsUsed 對(duì)應(yīng)于目前錢幣總數(shù)i所使用的硬幣數(shù)目

def coinChange(values,valuesCounts,money,coinsUsed):

#遍歷出從1到money所有的錢數(shù)可能

for cents in range(1,money+1):

minCoins = cents

#把所有的硬幣面值遍歷出來和錢數(shù)做對(duì)比

for kind in range(0,valuesCounts):

if (values[kind] <= cents):

temp = coinsUsed[cents - values[kind]] +1

if (temp < minCoins):

minCoins = temp

coinsUsed[cents] = minCoins

print ('面值:{0}的最少硬幣使用數(shù)為:{1}'.format(cents, coinsUsed[cents]))

13 廣度遍歷和深度遍歷二叉樹

給定一個(gè)數(shù)組,構(gòu)建二叉樹,并且按層次打印這個(gè)二叉樹

14 二叉樹節(jié)點(diǎn)

class Node(object):

def __init__(self, data, left=None, right=None):

self.data = data

self.left = left

self.right = right

tree = Node(1, Node(3, Node(7, Node(0)), Node(6)), Node(2, Node(5), Node(4)))

15 層次遍歷

def lookup(root):

row = [root]

while row:

print(row)

row = [kid for item in row for kid in (item.left, item.right) if kid]

16 深度遍歷

def deep(root):

if not root:

return

print root.data

deep(root.left)

deep(root.right)

if __name__ == '__main__':

lookup(tree)

deep(tree)

17 前中后序遍歷

深度遍歷改變順序就OK了

#coding:utf-8

#二叉樹的遍歷

#簡(jiǎn)單的二叉樹節(jié)點(diǎn)類

class Node(object):

def __init__(self,value,left,right):

self.value = value

self.left = left

self.right = right

#中序遍歷:遍歷左子樹,訪問當(dāng)前節(jié)點(diǎn),遍歷右子樹

def mid_travelsal(root):

if root.left is not None:

mid_travelsal(root.left)

#訪問當(dāng)前節(jié)點(diǎn)

print(root.value)

if root.right is not None:

mid_travelsal(root.right)

#前序遍歷:訪問當(dāng)前節(jié)點(diǎn),遍歷左子樹,遍歷右子樹

def pre_travelsal(root):

print (root.value)

if root.left is not None:

pre_travelsal(root.left)

if root.right is not None:

pre_travelsal(root.right)

#后續(xù)遍歷:遍歷左子樹,遍歷右子樹,訪問當(dāng)前節(jié)點(diǎn)

def post_trvelsal(root):

if root.left is not None:

post_trvelsal(root.left)

if root.right is not None:

post_trvelsal(root.right)

print (root.value)

18 求最大樹深

def maxDepth(root):

if not root:

return 0

return max(maxDepth(root.left), maxDepth(root.right)) + 1

19 求兩棵樹是否相同

def isSameTree(p, q):

if p == None and q == None:

return True

elif p and q :

return p.val == q.val and isSameTree(p.left,q.left) and isSameTree(p.right,q.right)

else :

return False

20 前序中序求后序

def rebuild(pre, center):

if not pre:

return

cur = Node(pre[0])

index = center.index(pre[0])

cur.left = rebuild(pre[1:index + 1], center[:index])

cur.right = rebuild(pre[index + 1:], center[index + 1:])

return cur

def deep(root):

if not root:

return

deep(root.left)

deep(root.right)

print root.data

21 單鏈表逆置

class Node(object):

def __init__(self, data=None, next=None):

self.data = data

self.next = next

link = Node(1, Node(2, Node(3, Node(4, Node(5, Node(6, Node(7, Node(8, Node(9)))))))))

def rev(link):

pre = link

cur = link.next

pre.next = None

while cur:

tmp = cur.next

cur.next = pre

pre = cur

cur = tmp

return pre

root = rev(link)

while root:

print root.data

root = root.next

22 兩個(gè)字符串是否是變位詞

class Anagram:

"""

@:param s1: The first string

@:param s2: The second string

@:return true or false

"""

def Solution1(s1,s2):

alist = list(s2)

pos1 = 0

stillOK = True

while pos1 < len(s1) and stillOK:

pos2 = 0

found = False

while pos2 < len(alist) and not found:

if s1[pos1] == alist[pos2]:

found = True

else:

pos2 = pos2 + 1

if found:

alist[pos2] = None

else:

stillOK = False

pos1 = pos1 + 1

return stillOK

print(Solution1('abcd','dcba'))

def Solution2(s1,s2):

alist1 = list(s1)

alist2 = list(s2)

alist1.sort()

alist2.sort()

pos = 0

matches = True

while pos < len(s1) and matches:

if alist1[pos] == alist2[pos]:

pos = pos + 1

else:

matches = False

return matches

print(Solution2('abcde','edcbg'))

def Solution3(s1,s2):

c1 = [0]*26

c2 = [0]*26

for i in range(len(s1)):

pos = ord(s1[i])-ord('a')

c1[pos] = c1[pos] + 1

for i in range(len(s2)):

pos = ord(s2[i])-ord('a')

c2[pos] = c2[pos] + 1

j = 0

stillOK = True

while j<26 and stillOK:

if c1[j] == c2[j]:

j = j + 1

else:

stillOK = False

return stillOK

print(Solution3('apple','pleap'))

23 動(dòng)態(tài)規(guī)劃問題

總結(jié)

以上是生活随笔為你收集整理的python 编程算法_python语言编程算法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。