LeetCode(2023-09-15)


524. 通过删除字母匹配到字典里最长单词

524. 通过删除字母匹配到字典里最长单词

贪心

class Solution:
    def findLongestWord(self, s: str, dictionary: List[str]) -> str:
        ans = ''
        for item in dictionary:
            p_item = 0
            for p_s in range(len(s)):
                if s[p_s] == item[p_item]:
                    p_item += 1
                if p_item == len(item):
                    ans = item if (len(item) > len(ans)) or (len(item) == len(ans) and item < ans) else ans
                    break
        return ans

566. 重塑矩阵

566. 重塑矩阵

class Solution:
    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
        nums = len(mat) * len(mat[0])
        if nums != r * c:
            return mat
        ans = [[0 for i in range(c)] for j in range(r)]
        row = 0
        col = 0
        for item in sum(mat, []):
            if col >= c:
                col = 0
                row += 1
            ans[row][col] = item
            col += 1
        return ans

1041. 困于环中的机器人

1041. 困于环中的机器人

class Solution:
    def isRobotBounded(self, instructions: str) -> bool:
        direction_steps = {1: 0, 2: 0, 3: 0, 4: 0}  # 1:north 2:east 3:south 4:west
        direction = 1
        direction_count = {'L': 0, 'R': 0}
        for instruction in instructions:
            if instruction == 'L' or instruction == 'R':
                direction_count[instruction] += 1
                direction = self.changeDirection(direction, instruction)
            else:
                direction_steps[direction] += 1
        n_s = direction_steps[1] - direction_steps[3]
        e_w = direction_steps[2] - direction_steps[4]
        if n_s == 0 and e_w == 0:
            return True
        if n_s >= 0 and direction == 3:
            return True
        if n_s <= 0  and direction == 3:
            return True
        if direction == 2 or direction == 4:
            return True
        return False

    def changeDirection(self, direction, instruction):
        if instruction == 'L':
            return direction - 1 if direction - 1 >= 1 else 4
        else:
            return direction + 1 if direction + 1 <= 4 else 1

2527. 查询数组 Xor 美丽值

2527. 查询数组 Xor 美丽值

查询数组 Xor 美丽值(根据对称性化简🤣)

class Solution:
    def xorBeauty(self, nums: List[int]) -> int:
        ans = 0
        for item in nums:
            ans ^= item
        return ans

LCP 68. 美观的花束

LCP 68. 美观的花束

双指针

from typing import List
from collections import defaultdict

class Solution:
    def beautifulBouquet(self, flowers: List[int], cnt: int) -> int:
        ans = 0
        counter = defaultdict(int)
        left = 0
        for right in range(len(flowers)):
            counter[flowers[right]] += 1
            while counter[flowers[right]] > cnt:
                counter[flowers[left]] -= 1
                left += 1
            ans += right - left + 1

        return ans % (10 ** 9 + 7)

声明:Hello World|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - LeetCode(2023-09-15)


我的朋友,理论是灰色的,而生活之树是常青的!