作者sustainer123 (溫水佳樹的兄長大人)
看板Marginalman
標題Re: [閒聊] 每日leetcode
時間Mon Oct 7 18:29:22 2024
※ 引述《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)
刷點白癡題目回手感 最近越來越不會刷題
寫啥都卡卡的 慘
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.194.160.111 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1728296964.A.076.html
→ Sougou: 哲學系的都會刷LeetCode,太卷了 10/07 18:29
→ JIWP: 我好佩服你 10/07 18:30
→ oin1104: 我好崇拜你 10/07 18:31
→ sustainer123: 你是大師 我最近快兩百天的紀錄都斷了 10/07 18:31
→ JIWP: 花錢補回來阿 10/07 18:32
→ sustainer123: 有點多:)) 10/07 18:36