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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

常见的面试题整理 -python

發布時間:2024/7/5 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 常见的面试题整理 -python 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

常見的面試題整理

在這里插入代碼片 #二分查找def binarySearch(alist, item):first=0;last=len(alist)-1;while first <= last:mid=(first+last)/2;print(mid)if alist[mid]>item:last=mid-1;elif alist[mid]<item:first=mid+1return -1test=[0,1,2,8,13,17,19,9,0]print(binarySearch(test,3))#冒泡排序 def bubbleSort(alist):for passnum in range(len(alist)-1,0,-1):for i in range(passnum)if alist[i]>alist[i+1]:alist[i],alist[i+1]=alist[i+1],alist[i]return alist#插入排序def insertionSort(alist):for index in range(1,len(alist)):currentValue=alist[index]position=indexwhile position>0 and alist[position-1]>currentValue:alist[position]=alist[position-1]position-=1alist[position]=currentValue#快速排序 def quickSort(alist):quickSortHelper(alist,0,len(alist)-1)def quickSortHelper(alist,first,last):if first<last:splitPoint=partition(alist,first,last)quickSortHelper(alist,first,splitPoint-1)quickSortHelper(alist,splitPoint+1,last)def partition(alist,first,last)item=alist[first]left=first+1right=lastwhile left<right:while alist[left]<=item and left<=rightleft +=1alist[left],alist[right]=alist[right],alist[left]while alist[right]>=item and left<=right:right -=1alist[left],alist[right]=alist[right],alist[left]return right#return left#樹的四種遍歷 class node(object):def __init__(self,data=None,left=None,right=None):self.data=dataself.left=leftself.right=right#深度def depth(tree):if tree== None:return 0left,right=depth(tree.left),depth(tree.right)return max(left,right)+1#前序遍歷def pre_order(tree):if tree==None:returnprint tree.datapre_order(tree.left)pre_order(tree.right)#中序遍歷def min_order(tree):if tree== None:returnmin_order(tree.left)print(tree.data)min_order(tree.right)#后序遍歷def post_order(tree):if tree==Nonereturnpost_order(tree.left)post_order(tree.right)print(tree.data)#層次遍歷def level_order(tree):if tree==Nonereturnq=[]q.append(tree)while q:current=q.pop(0)print current.dataif current.left != None:q.append(current.left)if current.right !=None:q.append(current.right)#括號匹配 def match_parentTheses(s):ls=[]parentTheses="()[]{}"for i in range(len(s)):si=s[i]if parentTheses.find(si) == -1:continueif si == "(" or si=="{" or si=="[":ls.append(si)continueif len(ls)==0:return Falsep=ls.pop()if (p == '(' and si == ')') or (p == '[' and si == ']') or (p == '{' and si == '}'):continueelse:return Falseif len(ls)>0:return Falsereturn True#反轉鏈表 def ReverseList(self,pHead):if not pHead or not pHead.next:return pHeadlast=Nonewhile pHead:tmp=pHead.nextpHead.next=lastlast=pHeadpHead=tmpreturn last#鏈表是否有環 def hasCycle(self,head):flag=Falseif head is None or head.next is None or head.next.next is None:return flag#對slow,fast進行初始化fast = head.next.nextslow=head.nextwhile fast is not slow:if fast.next is None or fast.next.next is None:return flagfast=fast.next.nextslow=slow.nextif fast is slow:flag=Truereturn flagif __name__=='__main__'pass#歸并排序 def merge(L,R):n=len(L)+len(R)L.append(float("inf"))R.append(float("inf"))i=0j=0A=[]for k in range(0,n):if(L[i]<R[j])A.append(L[i])i=i+1else:A.append(R[j])j=j+1return A merge([1,5,6],[2,3]) def merge_sort(A)l=len(A)if l<=1:return Aelse:mid=l/2print midleft=merge_sort(A[0:mid])right=merge_sort(A[mid:])return merge(left,right)merge_sort([3,2,1,0,5,8,7,2,-5,9,6,11])#改寫歸并排序 def merge(left,right):res=[]while left and right:min_val=left.pop(0) if left[0] < right[0] else right.pop(0)res.append(min.val)res += left if left else rightreturn resdef merge_sort(A):if len(A)<=1:res=Aelse:mid=len(A)/2left,right=merge_sort(A[:mid]),merge_sort(A[mid:])res=merge(left,right)return res

動態規劃:https://blog.csdn.net/zichen_ziqi/article/details/82184495
1、硬幣問題

2、爬樓梯問題(青蛙跳臺問題)

3、裝箱問題與背包問題

4、最大遞增子序列問題(最長上升子序列問題)

5、最長公共子序列問題

6、最長公共子串問題

7、最大連續子序列求和問題(最大子串求和問題)

8、股票收益最大化(一次交易、多次交易與最多兩次交易)

版權聲明:本文為博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。 本文鏈接:https://blog.csdn.net/zichen_ziqi/article/details/82184495

? ? ? ? ? ? ? ? ? ? ? ? ? ? ??在線編程——動態規劃常見的面試問題總結(Python)

總結

以上是生活随笔為你收集整理的常见的面试题整理 -python的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。