Re: [閒聊] 每日leetcode
看板Marginalman作者DJYOSHITAKA (franchouchouISBEST)時間1年前 (2024/10/07 23:31)推噓2(2推 0噓 0→)留言2則, 2人參與討論串960/1548 (看更多)
就用stack掃過去
要檢查一下stk.empty()
不然會吃index out of range
def minLength(self, s: str) -> int:
stk = []
for c in s:
if c=='B' and len(stk)>0 and stk[-1]=='A':
stk.pop()
elif c=='D' and len(stk)>0 and stk[-1]=='C':
stk.pop()
else:
stk.append(c)
return len(stk)
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.229.37.69 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1728315071.A.0E2.html
推
10/07 23:34,
1年前
, 1F
10/07 23:34, 1F
推
10/07 23:36,
1年前
, 2F
10/07 23:36, 2F
討論串 (同標題文章)
完整討論串 (本文為第 960 之 1548 篇):