Re: [闲聊] 每日leetcode

楼主: sustainer123 (caster)   2024-10-07 18:29:22
※ 引述《JerryChungYC (JerryChung)》之铭言:
: https://leetcode.com/problems/minimum-string-length-after-removing-substrings
: 2696. Minimum String Length After Removing Substrings
: 有一个由大写英文字母组成的字串 s
: 每次操作可以将字串中出现的"AB"或"CD"删除
: 回传结果字串的最小可能长度
: Note: 删除子字串后字串会连接起来 可能产生新的"AB"或"CD"
: Example 1:
: Input: s = "ABFCACDB"
: Output: 2
: Explanation: 移除"AB" > 移除"CD" > 移除"AB" > 剩下"FC" 回传2
: Example 2:
: Input: s = "ACBBD"
: Output: 5
: Explanation: 没有可删除的"AB"或"CD" 回传5
: Constraints:
: 1 <= s.length <= 100
: s 只包含大写英文字母
: 思路:删除
: Python Code:
: class Solution:
: def minLength(self, s: str) -> int:
: while 'AB' in s or 'CD' in s:
: s = s.replace('AB', '')
: s = s.replace('CD', '')
: return len(s)
: 直接用两次replace跟两者各多用一次 if ... in s: 哪种比较好啊 会更快吗
: 看了一下今天的太简单没人想写 只好由最底层的我来了
思路:
一样
Python Code:
class Solution:
def minLength(self, s: str) -> int:
while "AB" in s or "CD" in s:
s = s.replace("AB", "")
s = s.replace("CD", "")
return len(s)
刷点白痴题目回手感 最近越来越不会刷题
写啥都卡卡的 惨
作者: Sougou (搜狗)   2023-10-07 18:29:00
哲学系的都会刷LeetCode,太卷了

Links booklink

Contact Us: admin [ a t ] ucptt.com