面试基本数据结构和算法

1.排序算法

1.1快排

def quick_sort(li, start, end):
    # 分治 一分为二
    # start=end ,证明要处理的数据只有一个
    # start>end ,证明右边没有数据
    if start >= end:
        return
    # 定义两个游标,分别指向0和末尾位置
    left = start
    right = end
    # 把0位置的数据,认为是中间值
    mid = li[left]
    while left < right:
        # 让右边游标往左移动,目的是找到小于mid的值,放到left游标位置
        while left < right and li[right] >= mid:
            right -= 1
        li[left] = li[right]
        # 让左边游标往右移动,目的是找到大于mid的值,放到right游标位置
        while left < right and li[left] < mid:
            left += 1
        li[right] = li[left]
    # while结束后,把mid放到中间位置,left=right
    li[left] = mid
    # 递归处理左边的数据
    quick_sort(li, start, left-1)
    # 递归处理右边的数据
    quick_sort(li, left+1, end)
 
if __name__ == '__main__':
    l = [6,5,4,3,2,1]
    # l = 3 [2,1,5,6,5,4]
    # [2, 1, 5, 6, 5, 4]
    quick_sort(l,0,len(l)-1)
    print(l)
    # 稳定性:不稳定
    # 最优时间复杂度:O(nlogn)
    # 最坏时间复杂度:O(n^2)

1.2 堆排

def heapify(arr, n, i): 
    largest = i  
    l = 2 * i + 1     # left = 2*i + 1 
    r = 2 * i + 2     # right = 2*i + 2 
  
    if l < n and arr[i] < arr[l]: 
        largest = l 
  
    if r < n and arr[largest] < arr[r]: 
        largest = r 
  
    if largest != i: 
        arr[i],arr[largest] = arr[largest],arr[i]  # 交换
  
        heapify(arr, n, largest) 
  
def heapSort(arr): 
    n = len(arr) 
  
    # Build a maxheap. 
    for i in range(n, -1, -1): 
        heapify(arr, n, i) 
  
    # 一个个交换元素
    for i in range(n-1, 0, -1): 
        arr[i], arr[0] = arr[0], arr[i]   # 交换
        heapify(arr, i, 0)

2. 二叉树

2.1 二叉树的层次遍历

def levelorder(root):
    res,queue=[],[root]
    if not root:
        return res
    while queue:
        node=queue.pop(0)
        if node.left:
            queue.append(node.left)
        if node.right:
            queue.append(node.right)
        res.append(node)
    return res

2.2 递归完成二叉树的前,中,后序遍历

#前序遍历
def preorder(root):
    res=[]
    if not root:
        return None
    res.append(root.val)
    preorder(root.left)
    preorder(root.right)
    return res
#中序遍历
def inorder(root):
    res=[]
    if not root:
        return res
    inorder(root.left)
    res.append(root.val)
    inorder(root.right)
    return res
#后序遍历
def lastorder(root):
    res=[]
    if not root:
        return res
    lastorder(root.left)
    lastorder(root.right)
    res.append(root.val)
    return res

2.3 非递归完成二叉树的前,中,后序遍历

#非递归前序
def preorder(root):
    res,stack=[],[root]
    if not root:
        return res
    while stack:
        node=stack.pop()
        res.append(node.val)
        if node.right:
            stack.append(node.right)
        if node.left:
            stack.append(node.left)
    return res
 #非递归中序
 def inorder(root):
    stack=[]
    node=root
    res=[]
    while stack or node:
        while node:
            stack.append(node)
            node=node.left
        node=stack.pop()
        res.append(node.val)
        node=node.right
    return res
 #非递归后序
def lastorder(root):
    res=[]
    if not root:
        return None
    stack=[root]
    while stack:
        node=stack.pop()
        if node.left:
            stack.append(node.left)
        if node.right:
            stack.append(node.right)
        res.append(node.val)
    return res[::-1]

3. 链表反转

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        '''
        p,rev=head,None
        while p:
            rev,rev.next,p=p,rev,p.next
        return rev
        '''
        if not head or not head.next:
            return head
        pre=None
        while head:
            tmp=head.next
            head.next=pre
            pre=head
            head=tmp
        return pre
    
    #递归方法
    if not head or not head.next:
        return head
    new_node=self.reverseList(head.next)
    head.next.next=head
    head.next=None
    return new_head
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值