关于python:力扣周赛-198-python-解答

34次阅读

共计 2268 个字符,预计需要花费 6 分钟才能阅读完成。

做出了四道题目,但后两道做的很勉强,也错了几次。刚看第三道题,尽管写着 medium,然而没思路,关上第四道看到反而曾经有人过了,于是先做了第四道,才回来做第一道。

最初一道是纯暴力枚举加了几个条件:

  1. 去除相邻反复值
  2. 是函数输入为 0 时进行枚举,因为 0 按位与任何数都仍然是 0.
  3. 因为在一直按位与的过程中函数输入是枯燥减的,所以,但差值大于以后最小差值也能够进行遍历

然而我感觉应该还有更好的解法

第三题用的模仿办法,顺次寻找只含一个字母,两个字母 … 的串

5464 换酒问题

https://leetcode-cn.com/conte…

class Solution:
    def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
        c = numBottles
        cc = numBottles
        kp = numBottles
        while kp >= numExchange:
            cc = kp // numExchange
            kp = kp % numExchange + cc
            c += cc
        return c

5465 子树中标签雷同的节点数

https://leetcode-cn.com/conte…

class Solution:
    def countSubTrees(self, n: int, edges: List[List[int]], labels: str) -> List[int]:
        ed = {}
        for s, e in edges:
            if s not in ed:
                ed[s] = []
            if e not in ed:
                ed[e] = []
            ed[s].append(e)
            ed[e].append(s)
        ans = [0] * n
        vised = [False] * n
        def vis(i):    
            vised[i] = True
            c = {}
            c[labels[i]] = 1
            if i in ed:
                for ch in ed[i]:
                    if not vised[ch]:
                        cc = vis(ch)
                        for k in cc:
                            if k not in c:
                                c[k] = 0
                            c[k] += cc[k]
            ans[i] = c[labels[i]]
            return c
        vis(0)
        return ans

5466 最多的不重叠子字符串

https://leetcode-cn.com/conte…

class Solution:
    def maxNumOfSubstrings(self, s: str) -> List[str]:
        d = {}
        for i, ch in enumerate(s):
            if ch not in d:
                d[ch] = [i, i, True]
            else:
                if d[ch][1] != i - 1:
                    d[ch][2] = False
                d[ch][1] = i
        #print(d)
        used = {}
        ans = []
        nd = []
        for ch in d:
            if d[ch][2]:
                used[ch] = True
                ans.append(s[d[ch][0]:d[ch][1]+1])
        for ch in d:
            if not d[ch][2]:
                cc = 0
                ccx = set()
                f = True
                
                mini = d[ch][0]
                maxi = d[ch][1]
                change = True
                while change:
                    change = False
                    i = d[ch][0]
                    while i <= maxi:
                        if s[i] in used:
                            f = False
                            break
                        else:
                            if maxi < d[s[i]][1]:
                                change = True
                                maxi = d[s[i]][1]
                            if mini > d[s[i]][0]:
                                mini = d[s[i]][0]
                                change = True
                            ccx.add(s[i])
                        i += 1
                    i = d[ch][0]
                    while i >= mini:
                        if s[i] in used:
                            f = False
                            break
                        else:
                            if maxi < d[s[i]][1]:
                                change = True
                                maxi = d[s[i]][1]
                            if mini > d[s[i]][0]:
                                mini = d[s[i]][0]
                                change = True
                            ccx.add(s[i])
                        i -= 1
                d[ch][1] = maxi
                d[ch][0] = mini
                if f:
                    nd.append([len(ccx), maxi-d[ch][0], ch, list(ccx)])
        #print(d)
        nd.sort()
        for n in nd:
            f = True
            for cc in n[3]:
                if cc in used:
                    f = False
                    break
            if f:
                for cc in n[3]:
                    used[cc] = True
                ch = n[2]
                ans.append(s[d[ch][0]:d[ch][1]+1])
        return ans

5467 找到最靠近目标值的函数值

https://leetcode-cn.com/conte…

class Solution:
    def closestToTarget(self, arr: List[int], target: int) -> int:
        arr2 = []
        l = arr[0] -1
        for v in arr:
            if v != l:
                arr2.append(v)
                l = v
        #print(len(arr), len(arr2))
        arr = arr2
        r = abs(-1000000000 - target)
        for i in range(len(arr)):
            ans = arr[i]
            r = min(r, abs(ans - target))
            for j in range(i+1, len(arr)):
                ans = ans & arr[j]
                r = min(r, abs(ans - target))
                if ans == 0 or r == 0:
                    break
                if target > ans and target - ans >= r:
                    break
            if r == 0:
                break
        return r

欢送来我的博客刷题分类(有刷题和口试内容):https://es2q.com/blog/categor…

正文完
 0