【python面試】數據結構和算法

1.常見的排序算法?

a.冒泡排序算法

def bubble_sort(alist):
    for i in range(len(alist)-1,0,-1):
        for j in range(i):
            if alist[j]>alist[j+1]:
                alist[j],alist[j+1]=alist[j+1],alsit[j]
    return alist
複製代碼

b.快速排序bash

def quick_sort(alist):
    if len(alist) >=2:
        mid = alist[len(alist)//2]
        left,right = [],[]
        alist.remove(mid)
        for num in alist:
            if mid > num:
                left.append(num)
            else:
                right.append(num)
        return quick_sort(left)+[mid]+quick_sort(right)
    else:
        return alist
        
複製代碼

c.選擇排序app

def selection_sort(alist):
    for i in range(len(alist)-1):
        min_index = i
        for j in range(i+1,len(alist)):
            if alist[min_index] > alist[j]:
                min_index = j
        if min_index != i:
            alist[min_index],alist[i] = alist[i],alist[min_index]
    return alist
複製代碼

d.插入排序post

def insert_sort(alist):
    for i in range(1,len(alist)):
        for j in range(i,0,-1):
            alist[j],alist[j-1]=alist[j-1],alist[j]
    return alist
複製代碼

排序算法講解:juejin.im/post/5c19a1…ui

2.二分查找

def binary_search(alist,num):
    if len(alist) > 1:
        mid = len(alist)//2
        if alist[mid] == num:
            return True
        elif alist[mid] > num:
            return binary_search(alist[:mid],num)
        else:
            return binary_search(alist[mid+1:],num)
    else:
        return False

複製代碼

3.隊列和棧

a.隊列的實現spa

class Queue(object):
    '''隊列'''
    def __init__(self):
        self.items = []

    def is_empty(self):
        return self.items == []

    def push(self,item):
        self.items.insert(0,item)

    def pop(self):
        return self.items.pop()

    def size(self):
        return len(self.items)

    def clear(self):
        '''清空隊列'''
        del self.items
複製代碼

b.棧的實現code

class Stack(object):
    '''棧'''
    def __init__(self):
        self.items = []

    def is_empty(self):
        return self.items == []

    def push(self,item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def size(self):
        return len(self.items)

    def clear(self):
        '''狀況棧'''
        del self.items

複製代碼

c.兩個隊列實現一個棧排序

隊列a爲進棧隊列,若是隊列a中只有一個元素直接返回,隊列a中有多個元素,循環遍歷,將其餘元素放到隊列b中,直到隊列a中只有一個元素,返回,交換隊列a,b,以便下次獲取隊列

class TwoQueueToStack(object):
    '''兩個隊列實現一個棧'''
    def __init__(self):
        self.queue_a = Queue()
        self.queue_b = Queue()

    def push(self,item):
        self.queue_a.put(item)

    def pop(self):
        while self.queue_a.qsize() > 1:
            self.queue_b.put(self.queue_a.get())
        if self.queue_a.qsize() == 1:
            res = self.queue_a.get()
            self.queue_a,self.queue_b = self.queue_b,self.queue_a
            return res

複製代碼

d.一個隊列實現一個棧rem

# 一個隊列實現一個棧
class OneQueueToStack(object):
    def __init__(self):
        self.queue = Queue()

    def push(self,item):
        self.queue.put(item)

    def pop(self):
        count = self.queue.qsize()
        if count == 0:
            return False
        while count > 1:
            x = self.queue.get()
            self.queue.put(x)
            count -= 1
        return self.queue.get()
複製代碼
相關文章
相關標籤/搜索