Re: [闲聊] 每日leetcode

楼主: DJYOSHITAKA (Evans)   2024-10-07 23:31:08
就用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)
作者: sustainer123 (caster)   2024-10-07 23:34:00
剩我只会replace
作者: CP3isgood (3345678)   2024-10-07 23:36:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com