Re: [閒聊] 每日leetcode
921. Minimum Add to Make Parentheses Valid
## 思路
字串只有左右括號
所以直接預設要加n個括號, 有valid的pair就-2
## Code
```python
class Solution:
def minAddToMakeValid(self, s: str) -> int:
res = len(s)
left = 0
for ch in s:
if ch == '(':
left += 1
elif left:
left -= 1
res -= 2
return res
```
--
https://i.imgur.com/kyBhy6o.jpeg

--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 185.213.82.179 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1728437682.A.F7A.html
→
10/09 09:38,
1年前
, 1F
10/09 09:38, 1F
討論串 (同標題文章)
完整討論串 (本文為第 966 之 1550 篇):