From 17b63f1e7af61afba34386b86ae450ee4095c05e Mon Sep 17 00:00:00 2001 From: xqm32 <458173774@qq.com> Date: Wed, 13 Sep 2023 21:58:06 +0800 Subject: [PATCH] leetcode: finished #921 --- content/leetcode/2023/9.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/content/leetcode/2023/9.md b/content/leetcode/2023/9.md index 46c43c4b8..16e511650 100644 --- a/content/leetcode/2023/9.md +++ b/content/leetcode/2023/9.md @@ -3,6 +3,34 @@ title: 2023.9 draft: false --- +# 2023.9.13 + +```python +# +# @lc app=leetcode.cn id=921 lang=python3 +# +# [921] 使括号有效的最少添加 +# + + +# @lc code=start +class Solution: + def minAddToMakeValid(self, s: str) -> int: + stack = [] + for i in s: + if i == "(": + stack.append(i) + else: + if stack and stack[-1] == "(": + stack.pop() + else: + stack.append(i) + return len(stack) + + +# @lc code=end +``` + # 2023.9.12 ```python