数据结构&算法积累

1 快速排序

参考:https://blog.csdn.net/weixin_43250623/article/details/88931925


def quick_sort(nums, start, end):
    if start > end:
        return
    mid = nums[start]
    low = start
    high = end 
    while low < high:
        while low < high and nums[high] > mid:
            high = high - 1 
        nums[low] = nums[high]
        while low < high and nums[low] < mid:
            low = low + 1 
        nums[high] = nums[low]
    nums[low] = mid 
    quick_sort(nums, start, low - 1)
    quick_sort(nums, low + 1, end)


if __name__ == "__main__":
   a = [10, 9, 8, 7, 6]
   quick_sort(a, 0, len(a) - 1)
   print a

2 归并排序

参考:https://blog.csdn.net/hellojoy/article/details/81835559


def merge(left, right):
    retData = []
    i = 0
    j = 0
    while i < len(left) and j < len(right):
        if left[i] <= right[j]:
            retData.append(left[i])
            i = i + 1 
        else:
            retData.append(right[j])
            j = j + 1 
    while i < len(left):
        retData.append(left[i])
        i = i + 1 
    while j < len(right):
        retData.append(right[j])
        j = j + 1 
    return retData


def merge_sort(nums):
    if len(nums) == 1:
        return nums
    mid = len(nums) / 2 
    left = merge_sort(nums[0:mid])
    right = merge_sort(nums[mid: len(nums)])
    return merge(left, right)

if __name__ == "__main__":
    a = [10, 9, 8]
    b = merge_sort(a)
    print b

3 动态规划

最关键的是写出状态转换方程,也就是递归方程。

参考:https://blog.csdn.net/zw6161080123/article/details/80639932

面试题:

两个有序数组合并成一个
两个有序链表合并成一个
链表反转
删除所有的驼峰字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值