面试高频leetcode算法题

文章目录

  • 快速排序
  • 二分查找
  • 爬楼梯
  • 两数之和
  • 最大回撤
  • 合并两个有序数组
  • 最大连续子数和
  • 最长不重复子串
  • 全排列
  • 三数之和

持续更新

快速排序

def quick_rank(nums):
    if len(nums) <= 1:
        return nums
    left,right = [],[]
    mid = nums[0]
    for i in nums[1:]:
        if i > mid:
            right.append(i)
        else:
            left.append(i)
    return quick_rank(left)+ [mid]+quick_rank(right)

二分查找

给定一个有序数组 arr 和一个目标元素 target ,返回该 target 在 arr 中的索引,若不存在,返回-1

def binary_search(nums,target):
    left,right = 0, len(nums) - 1
    while left <= right:
        mid = (left+right)//2
        if nums[mid] == target:
            return mid
        if nums[mid] < target:
            left = mid + 1 
        else:
            right = mid - 1
    return -1

爬楼梯

有一个楼梯,总共有10级台阶,每次只能走一级或者两级台阶,全部走完,有多少种走法

def pa_stairs(n):
    if n == 1:
        return 1
    if n == 2:
        return 2
    a,b = 1,2
    for _ in range(2,n):
        a,b = b,a+b
    return b

两数之和

寻找列表中满足两数之和等于目标值的元素的下标。例如:arr = [2,7,4,9],target = 6 则返回 [0,2],若不存在,返回空列表[]

def sum_of_two(nums,target):
    dic = {}
    for i in range(len(nums)):
        cur = target - nums[i]
        if cur in dic.keys():
            return [dic[cur],i]
        else:
            dic[nums[i]] = i
    return []

最大回撤

有一个数组,x的索引小于y的索引,使得 x-y 最大, 求最大回撤值。例如 arr = [3,7,2,6,4,1,9,8,5], 最大回撤是6

def max_drawdown(nums):
    if len(nums) == 1:
        return nums[-1]
    re = float('-inf')
    min_x = nums[-1]
    for i in range(len(nums)-2,-1,-1):
        min_x = min(nums[i],min_x)
        re = max(nums[i]-min_x,re)
    return re

合并两个有序数组

给定两个按升序排列的有序数组,将它们合并成一个新的有序数组。例如:a = [1,2,6,8], b = [2,4,7,10],输出为 arr = [1,2,2,4,6,7,8,10]

def merge_sorted_array(a,b):
    re = []
    while a and b:
        if a[0] > b[0]:
            re.append(b.pop(0))
        else:
            re.append(a.pop(0))
    if a:
        re.extend(a)
    else:
        re.extend(b)
    return re

最大连续子数和

给定一个数组,求其最大连续子数组的和。例如:arr = [1,5,-10,2,5,-3,2,6,-3,1]. 输出为:12。对应的连续子数组为 [2,5,-3,2,6]

def max_sub(nums):
    n = len(nums)
    if n == 1:
        return nums[0]
    dp,re = nums[0],nums[0]
    for i in range(1,len(nums)):
        dp = max(nums[i],dp+nums[i])
        re = max(re,dp)
    return re

最长不重复子串

给定一个字符串,找出没有重复字符的最长的子串。例如输入“abcbefgf”,答案是 “cbefg”

def longest_substr(s):
    n = len(s)
    if n == 1:
        return s
    left,right = 0,1
    memo = set(s[0])
    re = s[0]
    while left <= right and right < n:
        if s[right] not in memo:
            memo.add(s[right])
            right += 1
            cur = s[left:right]
            if len(cur) > len(re):
                re = cur
        else:
            memo.remove(s[left])
            left += 1
    return re

全排列

给定一个数组,找出其所有可能的排列。例如: arr = [1,1,3],输出为 [[1,1,3],[1,3,1],[3,1,1]]

def permutations(nums):
    re = []
    n = len(nums)
    if n == 1:
        return [nums]
    def helper(nums,temp):
        if not nums:
            if temp not in re:
                re.append(temp)
            return
        for i in range(len(nums)):
            helper(nums[:i]+nums[i+1:],temp + [nums[i]])
    helper(nums,[])
    return re

三数之和

给定一个数组和目标数target,找出数组中a,b,c满足 a+b+c = target 的所有组合。例如:arr = [-3,-1,-2,1,2,3],target = 0。输出为 [(-3,1,2),(-2,-1,3)]。时间复杂度要求为O(n**2) ,空间复杂度要求O(1)

def sum_of_three(nums,target):
    nums = sorted(nums)
    n = len(nums)
    re = [ ]
    for k,v in enumerate(nums):
        i,j = k+1,n-1
        while i<j:
            if v+nums[i]+nums[j] < target:
                i += 1
            elif v+nums[i]+nums[j] > target:
                j -= 1
            else:
                re.append((v,nums[i],nums[j]))
                i,j = i+1,j-1
    return re

你可能感兴趣的:(leetcode)