LeetCode(2023-10-23)


LCR 068. 搜索插入位置

LCR 068. 搜索插入位置

from typing import List
import bisect


class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        return bisect.bisect_left(nums, target)

2825. 循环增长使字符串子序列等于另一个字符串

2825. 循环增长使字符串子序列等于另一个字符串

class Solution:
    def canMakeSubsequence(self, str1: str, str2: str) -> bool:
        p1 = 0
        p2 = 0
        while p1 < len(str1) and p2 < len(str2):
            if str1[p1] == str2[p2] or self.next_c(str1[p1]) == str2[p2]:
                p1 += 1
                p2 += 1
            else:
                p1 += 1
        if p2 == len(str2):
            return True
        return False

    def next_c(self, c):
        if c == 'z':
            return 'a'
        return chr(ord(c) + 1)

LCR 139. 训练计划 I

LCR 139. 训练计划 I

from typing import List
from collections import deque


class Solution:
    def trainingPlan(self, actions: List[int]) -> List[int]:
        ans = deque([])
        for i in range(len(actions)):
            if actions[i] % 2 == 1:
                ans.appendleft(actions[i])
            else:
                ans.append(actions[i])
        return list(ans)

LCR 182. 动态口令

LCR 182. 动态口令

class Solution:
    def dynamicPassword(self, password: str, target: int) -> str:
        return password[target:] + password[:target]

30. 串联所有单词的子串

30. 串联所有单词的子串

from collections import Counter


class Solution:
    def findSubstring(self, s: str, words: List[str]) -> List[int]:
        c = Counter(words)
        word_length = len(words[0])
        length = len(words) * word_length
        ans = []
        for i in range(0, len(s) - length + 1):
            d = dict(c)
            for j in range(i, i + length, word_length):
                w = s[j: j + word_length]
                if w in d:
                    d[w] -= 1
                else:
                    break
            else:
                if not any(d.values()):
                    ans.append(i)
        return ans

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

转载:转载请注明原文链接 - LeetCode(2023-10-23)


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