leetcode刷题记录1151-1160 python版

前言

继续leetcode刷题生涯
这里记录的都是笔者觉得有点意思的做法
参考了好几位大佬的题解,感谢各位大佬

1154. 一年中的第几天

class Solution:
    def dayOfYear(self, data: str) -> int:
        year = int(data[0:4])
        month = int(data[5:7])
        day = int(data[8:])
        dic = [31,28,31,30,31,30,31,31,30,31,30,31]
        if year%400==0 or year%4==0 and year%100!=0:
            dic[1]=29
        return sum(dic[:month-1])+day

1155. 掷骰子的N种方法

class Solution:
    def numRollsToTarget(self, d: int, f: int, target: int) -> int:
        m = 10 ** 9 + 7
        dp = [[0] * (target + 1) for _ in range(d + 1)]
        dp[0][0] = 1
        for i in range(1, d + 1):
            for j in range(1, f + 1):
                for k in range(j, target + 1):
                    dp[i][k] = (dp[i][k] + dp[i - 1][k - j]) % m
        return dp[-1][-1]

1156. 单字符重复子串的最大长度

class Solution:
    def maxRepOpt1(self, text: str) -> int:
        if len(text) == 1:
            return 1
        if len(text) == 2:
            if text[0] == text[1]:
                return 2
            else:
                return 1
        import collections
        left = [1] * len(text)
        right = [1] * len(text)
        for i in range(1, len(text)):
            if text[i] == text[i - 1]:
                left[i] = 1 + left[i - 1]
        for i in range(len(text) - 2, -1, -1):
            if text[i] == text[i + 1]:
                right[i] = 1 + right[i + 1]
        counter = collections.Counter(text)
        res = 1
        for i in range(1, len(text) - 1):  # 由于要比较i-1和i+1,所以掐头去尾,所以最开始代码要把len==1 len==2情况都考虑全了
            if text[i - 1] != text[i + 1]:  # 当前字母左右两侧字母不相同,最大连续长度只能来自某一侧且需要判断是否还有可交换元素存在
                if counter[text[i - 1]] > left[i - 1]:
                    res = max(res, left[i - 1] + 1)
                if counter[text[i + 1]] > right[i + 1]:
                    res = max(res, right[i + 1] + 1)
            else:  # 当前字母左右两侧字母相同,最大连续长度由两侧序列共同组成,此时同样需要判断是否还有可交换元素存在, 对于“aaaaa”同样适用
                if counter[text[i + 1]] > (left[i - 1] + right[i + 1]):
                    res = max(res, left[i - 1] + 1 + right[i + 1])
                elif counter[text[i + 1]] == (left[i - 1] + right[i + 1]):
                    res = max(res, left[i - 1] + right[i + 1])
        return res

1157. 子数组中占绝大多数的元素

# 暴力
class MajorityChecker:

    def __init__(self, arr: List[int]):
        self.arr = arr

    def query(self, left: int, right: int, threshold: int) -> int:
        import collections
        count = collections.Counter(self.arr[left: right + 1])
        for num, ct in count.items():
            if ct >= threshold:
                return num
        return -1
# 二分
class MajorityChecker:

    def __init__(self, arr: List[int]):
        import collections
        self.idx = collections.defaultdict(list)
        for i, num in enumerate(arr):
            self.idx[num] += [i]

    def query(self, left: int, right: int, threshold: int) -> int:
        import bisect
        for num in self.idx:
            if len(self.idx[num]) >= threshold:
                l_idx = bisect.bisect_left(self.idx[num], left)
                r_idx = bisect.bisect(self.idx[num], right)
                if r_idx - l_idx >= threshold:
                    return num
        return -1
# 剪枝
class MajorityChecker:

    def __init__(self, arr: List[int]):
        import collections
        self.idx = collections.defaultdict(list)
        for i, num in enumerate(arr):
            self.idx[num] += [i]
        self.idx = sorted(self.idx.items(), key = lambda x: -len(x[1]))

    def query(self, left: int, right: int, threshold: int) -> int:
        import bisect
        for num, idxs in self.idx:
            if len(idxs) >= threshold:
                l_idx = bisect.bisect_left(idxs, left)
                r_idx = bisect.bisect(idxs, right)
                if r_idx - l_idx >= threshold:
                    return num
            else:
                break
        return -1

1160. 拼写单词

class Solution:
    def countCharacters(self, words: List[str], chars: str) -> int:
        res = 0
        nums = collections.Counter(chars)
        for w in words:
            c = collections.Counter(w)
            if all(c[i] <= nums[i] for i in c):
                res += len(w)
        return res

猜你喜欢

转载自blog.csdn.net/weixin_44604541/article/details/109128606