【力扣hot100】刷题笔记Day1

前言

  • 既然打算年后去找算法的实习,所以之后想直接改用python刷hot100了(新坑芜湖~),在B站大学找到这个刷题教程,先快速过一遍里面提到的python语法

Python数组

  • # 创建数组
    a = []
    # 添加元素,O(1)
    a.append(1)
    a.append(2)
    a.append(3)  # [1, 2, 3]
    # 插入元素,O(N)
    a.insert(2,99)  # [1, 2, 99, 3]
    # 访问元素,O(1)
    temp = a[2]  # 99
    # 更新元素,O(1)
    a[2] = 88  # 88
    # 删除元素,O(N)
    a.remove(88)  # [1, 2, 3]
    a.pop(1)  # 索引值,O(N)  # [1, 3]
    a.pop()  # 尾部,O(1)  # [1]
    # 获取数组长度
    size = len(a)
    # 遍历数组,O(N)
    for i in a:
        print(i)
    for index, element in enumerate(a):
        print("Index at ", index, "is ", element)
    for i in range(0, len(a)):
        print("i: ", i, "element: ", a[i])
    # 查找元素,O(N)
    index = a.index(2)  # 查找元素2所对应的索引
    # 数组排序,O(NlogN)
    a = [3, 1, 2]
    a.sort()  # a = [1, 2, 3]
    a.sort(reverse=True)  # a = [3, 2, 1]

Python链表

  • # 创建链表,O(1)
    linkedlist = deque()
    # 添加元素
    linkedlist.append(1)    # O(1)
    linkedlist.append(2)
    linkedlist.append(3)    # [1, 2, 3]
    linkedlist.append(2, 99) # O(N)  # [1, 2, 99, 3]
    # 访问元素,O(1)
    element = linkedlist[2]  # 99
    # 搜索元素,O(N)
    index = linkedlist.index(99)  # 查找元素99所对应的索引,2
    # 更新元素,O(N)
    linkedlist[2] = 88  # [1, 2, 88, 3]
    # 删除元素,O(N)
    del linkedlist[2]  # 删除索引为2的元素
    linkedlist.remove(88)  # 删除值为88的元素
    # 长度
    lenth = len(linkedlist)

Python队列

  • # 创建队列
    queue = deque()
    # 添加元素,O(1)
    queue.append(1)
    queue.append(2)
    queue.append(3)  # [1, 2, 3]
    # 获取即将出队元素,O(1)
    temp1 = queue[0]  # 1
    # 删除即将出队的元素,O(1)
    temp2 = queue.popleft()  # 1, [2, 3]
    # 队列长度判空,O(1)
    len(queue) == 0
    # 边删除边遍历队列,O(N)
    while len(queue) != 0:
        temp = queue.popleft()

Python栈

  • # 创建栈
    stack = []
    # 添加元素,O(1)
    stack.append(1)
    stack.append(2)
    stack.append(3)  # [1, 2, 3]
    # 获取栈顶元素,O(1)
    stack[-1]  # 3
    # 删除栈顶元素,O(1)
    temp = stack.pop()  # 2
    # 栈是否为空,O(1)
    len(stack) == 0
    # 边删除边遍历栈,O(N)
    while len(stack) > 0:
        temp = stack.pop()

Python哈希表

  • # 数组创建哈希表
    hashTable = ['']*4
    # 字典创建哈希表
    mapping = {}
    # 添加元素,0(1)
    hashTable[1] = 'hanmeimei'
    hashTable[2] = 'lihua'
    hashTable[3] = 'siyangyuan'
    mapping[1] = 'hanmeimei'
    mapping[2] = 'lihua'
    mapping[3] = 'siyangyuan'
    # 修改元素,0(1)
    hashTable[1] = 'bishi'
    mapping[1] = 'bishi'
    # 删除元素,0(1)
    hashTable[1] = ''
    mapping.pop(1)
    del mapping[1]
    # 获取元素,0(1)
    hashTable[3]
    mapping[3]
    # 检查key是否存在,0(1)
    3 in mapping
    # 哈希表长度判空,0(1)
    len(mapping) == 0

 Python集合

  • # 创建集合
    s = set()
    # 添加元素,O(1)
    s.add(10)
    s.add(3)
    s.add(5)
    s.add(2)  # 顺序不定,自动去重
    s.add(2)  # {2,10,3,5}
    # 搜索元素,O(1)
    2 in s
    # 删除元素,O(1)
    s.remove(2)  # {10,3,5}
    # 长度,O(1)
    len(s)

Python堆

  • import heapq
    # 创建最小堆
    # 最大堆的话需要元素都取反,按最小堆算,结果再取反
    minheap = []
    heapq.heapify(minheap)
    # 增加元素
    heapq.heappush(minheap,10);
    heapq.heappush(minheap,8);
    heapq.heappush(minheap,9);
    heapq.heappush(minheap,2);
    heapq.heappush(minheap,1);
    heapq.heappush(minheap,11);  # [1,2,9,10,8,11]
    # 堆顶元素
    minheap[0]
    # 删除堆顶,自动调整
    heapq.heappop(minheap)
    # 大小
    len(minheap)
    # 迭代
    while len(minheap) != 0:
        print(heapq.heappop(minheap))

后言

  •  下午又要干活+备课了,在家的时间利用要充实起来!

你可能感兴趣的:(力扣hot100刷题笔记,笔记,数据结构,leetcode,算法,职场和发展)