python面試總結4 演算法與內建資料結構

2022-07-25 04:48:05 字數 2993 閱讀 4923

演算法與內建資料結構

資料結構/演算法

語言內建

內建庫線性結構

list(列表)/tuple(元祖)

array(陣列,不常用)/collection.namedtuple

鏈式結構

collections.deque(雙端佇列)

字典結構

dict(字典)

collections.counter(計數器)/ordereddict(有序字典)

集合結構

set(集合)/frozenset(不可變集合)

排序演算法

sorted

二分演算法

bisect模組

堆演算法heapq模組

快取演算法

functors.lru_cache(least recent used,python3)

coolections模組提供了一些內建資料結構的擴充套件

collections

point = collections.namedtuple('point','x','y')

p = point(1,2)

namedtuple讓tuple屬性可讀

de = collections.deque()
c = collections.counter()

c = coolections.counter('abcab')

python dict 底層結構

dict底層使用的雜湊表

python list/tuple區別

t = ([1],2,3)

t([1,1],2,3)

儲存的引用不可變指的是你沒法替換掉這個物件,但是如果對系那個本身是乙個可變物件,是可以修改這個引用指向的可變物件的

什麼是lrucache?

least-recently-used 替換掉最近最少使用的物件

字典用來快取,迴圈雙端鍊錶用來記錄訪問順序

from collections import ordereddict

class lrucache:

def __init__(self, capacity=128):

self.od = ordereddict()

self.capacity = capacity

def get(self, key): #每次訪問更新最新使用的key

if key in self.od:

val = self.od[key]

self.od.move_to_end(key)

return val

else:

return -1

def put(self, key, value): # 更新k/v

if key in self.od:

del self.od[key]

self.od[key] = value # 更新key 到表頭

else: # insert

self.od[key] = value

# 判斷當前容量是否已經滿了

if len(self.od) > self.capacity:

self.od.popitem(last=false)

code/lrucache.py

演算法常考點

排序+查詢,重中之重

python web 後端常考資料結構

常考資料結構之鍊錶

鍊錶有單鏈表、雙鏈表、迴圈雙鏈表

資料結構之鍊錶

# definition for singly-linked list.

# class listnode:

# def __init__(self, x):

# self.val = x

# self.next = none

class solution:

def reverselist(self, head: listnode) -> listnode:

pre = none

cur = head

while cur:

nextnode = cur.next

cur.next = pre

pre = cur

cur = nextnode

ruture pre

資料結構之佇列

佇列(queue)是先進先出結構

from collections import deque

class queue:

def __init__(self):

self.items = deque()

def pop(self):

return self.items.popleft()

def empty(self):

return len(self.items) == 0

def test_queue():

q = queue()

print(q.pop())

print(q.pop())

print(q.pop())

test_queue()()01

2

常考資料結構之棧

棧(stack)是後進先出結構

from collections import deque

class stack(object):

def __init__(self):

self.deque = deque() # 或者用list

def push(self, value):

def pop(self):

return self.deque.pop()

乙個常考問題: 如何用兩個棧實現佇列?

常考資料結構之字典與集合

python dict/set 底層都是雜湊表

面試總結4

值傳遞 在方法的呼叫中,實參會把它的值傳遞給形參,形參只是用實參的值初始化乙個臨時的儲存單元,因此形參和實參雖然值相同,但是有不同的儲存單元,形參的改變不會影響實參的值 引用傳遞 在方法呼叫中。傳遞的是物件,這時形參和實參指向同乙個儲存單元,因此形參的改變會影響實參的值 初始化階段 檢查web伺服器...

演算法 面試總結

以位元組為例,面試基本就是演算法題 八股文 演算法 leetcode top100 劍指offer刷熟絕壁夠了,剩下就是看運氣了,運氣不好碰到憨憨面試官刷個500題可能也不夠 八股文 在lc上找10來篇自己崗位的面經,把上面的問題搞熟 這就是拿位元組offer的步驟。永遠不要等自己一切都準備好了再投...

python面試總結

乙個函式接收資料夾的名稱作為引數,返回該檔案中所有檔案的全路徑,請補全 def print directry contents spath import os files list os.listdir spath print files list files list for file in fi...