Re: [閒聊] 每日leetcode
看板Marginalman作者sustainer123 (溫水佳樹的兄長大人)時間1年前 (2024/10/07 18:29)推噓0(0推 0噓 6→)留言6則, 4人參與討論串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
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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 958 之 1548 篇):