做出了四道题目,但后两道做的很勉强,也错了几次。刚看第三道题,虽然写着 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...


sxwxs
292 声望21 粉丝

计算机专业学生