LeetCode 每日一题 2023/8/14-2023/8/20

记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步


目录

      • 8/14 617. 合并二叉树
      • 8/15 833. 字符串中的查找与替换
      • 8/16 2682. 找出转圈游戏输家
      • 8/17 1444. 切披萨的方案数
      • 8/18 1388. 3n 块披萨
      • 8/19 2235. 两整数相加
      • 8/20


8/14 617. 合并二叉树

dfs深搜

class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
def mergeTrees(root1, root2):
    """
    :type root1: TreeNode
    :type root2: TreeNode
    :rtype: TreeNode
    """
    def func(n1,n2):
        if not n1:
            return n2
        if not n2:
            return n1
        
        node = TreeNode(n1.val+n2.val)
        node.left = func(n1.left,n2.left)
        node.right = func(n1.right,n2.right)
        return node
    return func(root1,root2)




8/15 833. 字符串中的查找与替换

op存放该位置能替换的数值
从头遍历每个位置

def findReplaceString(s, indices, sources, targets):
    """
    :type s: str
    :type indices: List[int]
    :type sources: List[str]
    :type targets: List[str]
    :rtype: str
    """
    from collections import defaultdict
    n = len(s)
    op = defaultdict(list)
    for i,ind in enumerate(indices):
        op[ind].append(i)
    ans = []
    i = 0
    while i<n:
        tag = False
        if i in op:
            for ind in op[i]:
                if s[i:i+len(sources[ind])]==sources[ind]:
                    tag = True
                    ans.append(targets[ind])
                    i+=len(sources[ind])
                    break
        if not tag:
            ans.append(s[i])
            i+=1
    return "".join(ans)



8/16 2682. 找出转圈游戏输家

模拟

def circularGameLosers(n, k):
    """
    :type n: int
    :type k: int
    :rtype: List[int]
    """
    do = [False]*n
    cur = 0
    i=1
    while not do[cur]:
        do[cur]=True
        cur+=i*k
        cur%=n
        i+=1
    return [i+1 for i in range(n) if not do[i]]




8/17 1444. 切披萨的方案数

动态规划 dp[k][i][j] 表示把坐标(i,j)右下方切割成k块的方案

def ways(pizza, k):
    """
    :type pizza: List[str]
    :type k: int
    :rtype: int
    """
    mod = 10**9+7
    m,n=len(pizza),len(pizza[0])
    apples = [[0]*(n+1) for _ in range(m+1)]
    dp = [[[0 for j in range(n)] for i in range(m)] for _ in range(k+1)]
    
    for i in range(m-1,-1,-1):
        for j in range(n-1,-1,-1):
            apples[i][j] = apples[i][j+1]+apples[i+1][j]-apples[i+1][j+1]+(pizza[i][j]=='A')
            if apples[i][j]>0:
                dp[1][i][j] = 1 
            else:
                dp[1][i][j] = 0
    for t in range(1,k+1):
        for i in range(m):
            for j in range(n):
                for ii in range(i+1,m):
                    if apples[i][j]>apples[ii][j]:
                        dp[t][i][j] = (dp[t][i][j]+dp[t-1][ii][j])%mod
                for jj in range(j+1,n):
                    if apples[i][j]>apples[i][jj]:
                        dp[t][i][j] = (dp[t][i][j]+dp[t-1][i][jj])%mod
    return dp[k][0][0]



8/18 1388. 3n 块披萨

可转换为在3n个数中 选择n个不相邻的数 和最大
动态规划dp[i][j]表示前i个数选择j个不相邻的数 最大和

def maxSizeSlices(slices):
    """
    :type slices: List[int]
    :rtype: int
    """
    def func(slices):
        m = len(slices)
        n = (len(slices)+1)//3
        dp = [[float("-inf") for _ in range(n+1)] for _ in range(m)]
        dp[0][0] = 0
        dp[0][1] = slices[0]
        dp[1][0] = 0
        dp[1][1] = max(slices[0],slices[1])
        for i in range(2,m):
            dp[i][0] = 0
            for j in range(1,n+1):
                dp[i][j] = max(dp[i-1][j],dp[i-2][j-1]+slices[i])
        return dp[m-1][n]
    return max(func(slices[1:]),func(slices[0:-1]))



8/19 2235. 两整数相加

如题相加

def sum(num1, num2):
    """
    :type num1: int
    :type num2: int
    :rtype: int
    """
    return num1+num2




8/20





你可能感兴趣的:(Exercise,leetcode,算法,职场和发展)