Re: [閒聊] 每日leetcode

看板Marginalman作者 (溫水佳樹的兄長大人)時間1年前 (2024/10/07 18:29), 編輯推噓0(006)
留言6則, 4人參與, 1年前最新討論串958/1548 (看更多)
※ 引述《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

10/07 18:29, 1年前 , 1F
哲學系的都會刷LeetCode,太卷了
10/07 18:29, 1F

10/07 18:30, 1年前 , 2F
我好佩服你
10/07 18:30, 2F

10/07 18:31, 1年前 , 3F
我好崇拜你
10/07 18:31, 3F

10/07 18:31, 1年前 , 4F
你是大師 我最近快兩百天的紀錄都斷了
10/07 18:31, 4F

10/07 18:32, 1年前 , 5F
花錢補回來阿
10/07 18:32, 5F

10/07 18:36, 1年前 , 6F
有點多:))
10/07 18:36, 6F
文章代碼(AID): #1d0xW41s (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1d0xW41s (Marginalman)