letcode 2-day

https://www.cnblogs.com/liuzhen1995/p/13767751.html

https://github.com/ZLiu21/Algorithm-and-Learning/tree/master/1_%E5%89%91%E6%8C%87Offer

二分查找


def binary_find(arr, target):
    start = 0
    end = len(arr) - 1
    while start <= end:
        mid = (start + end) // 2
        if arr[mid] < target:
            start = mid + 1
        elif arr[mid] > target:
            end = mid - 1
        else:
            return mid
    return -1

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(binary_find(a, 10))


def find_dump(arr, target):
    l = 0
    r = len(arr) - 1
    while l < r:
        mid = (l + r) // 2
        if arr[mid] > target:
            r = mid - 1
        if arr[mid] < target:
            l = mid + 1
        else:
            m_l = mid
            while m_l > 0:
                if arr[m_l] == target:
                    m_l -= 1
                else:
                    break
            m_r = mid
            while m_r < len(arr):
                if arr[m_r] == target:
                    m_r += 1
                else:
                    break
            print(m_l + 1, m_r - 1)
        break


if __name__ == '__main__':
    find_dump([1, 2, 3, 4, 4, 4, 5], 4)

栈的压入和弹出序列

def IsPopOrder(pushV, popV):
    stack = []
    index = 0
    for item in pushV:
        stack.append(item)
        while stack and stack[-1] == popV[index]:
            stack.pop(-1)
            index += 1
    return not stack

你可能感兴趣的:(letcode 2-day)