From 0f5c166221a9160f9f805b89b245177ec0a23501 Mon Sep 17 00:00:00 2001 From: mike <59136831+Mike0121@users.noreply.github.com> Date: Fri, 26 Apr 2024 20:53:55 +0900 Subject: [PATCH 1/3] 20. Valid Parentheses.md --- .../20. Valid Parentheses.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 "\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/20. Valid Parentheses.md" diff --git "a/\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/20. Valid Parentheses.md" "b/\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/20. Valid Parentheses.md" new file mode 100644 index 0000000..f3fe40d --- /dev/null +++ "b/\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/20. Valid Parentheses.md" @@ -0,0 +1,24 @@ + +変数名に関して: +今回、stackはただのpopするためだけのものなので、stackでも良いかと思いました。 +またdictに関しては何を保存しているのかがわからなくなるので、bracket_があった方が良いと思います。 + +```python +class Solution: + def isValid(self, s: str) -> bool: + bracket_dict = {"(": ")", "{": "}", "[": "]"} + bracket_stack = [] + + for bracket in s: + if bracket in bracket_dict.keys(): + bracket_stack.append(bracket) + continue + + if bracket_stack == 0: + return False + + if bracket_dict[bracket_stack[-1]] == bracket: + bracket_stack.pop() + + return not bracket_stack +``` From da9528e049c52f187a4ecdecb4e6f57a5d57ac7d Mon Sep 17 00:00:00 2001 From: mike <59136831+Mike0121@users.noreply.github.com> Date: Fri, 26 Apr 2024 21:25:25 +0900 Subject: [PATCH 2/3] Update 20. Valid Parentheses.md --- .../20. Valid Parentheses.md" | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git "a/\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/20. Valid Parentheses.md" "b/\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/20. Valid Parentheses.md" index f3fe40d..d71e924 100644 --- "a/\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/20. Valid Parentheses.md" +++ "b/\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/20. Valid Parentheses.md" @@ -13,12 +13,14 @@ class Solution: if bracket in bracket_dict.keys(): bracket_stack.append(bracket) continue - - if bracket_stack == 0: + + if len(bracket_stack) == 0: + return False + + if bracket_dict[bracket_stack[-1]] != bracket: return False - if bracket_dict[bracket_stack[-1]] == bracket: - bracket_stack.pop() - + bracket_stack.pop() + return not bracket_stack ``` From 6b4fc1c1ced31352172335ef2a6b78702ecdf1b0 Mon Sep 17 00:00:00 2001 From: mike <59136831+Mike0121@users.noreply.github.com> Date: Tue, 30 Apr 2024 00:50:13 +0900 Subject: [PATCH 3/3] Create 20. Valid Parentheses 4th.md --- .../20. Valid Parentheses 4th.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 "\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/20. Valid Parentheses 4th.md" diff --git "a/\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/20. Valid Parentheses 4th.md" "b/\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/20. Valid Parentheses 4th.md" new file mode 100644 index 0000000..7b5871d --- /dev/null +++ "b/\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/20. Valid Parentheses 4th.md" @@ -0,0 +1,22 @@ +時間: 3m51s
+Error: 1 (continue漏れ)
+時間計算量: O(N)
+空間計算量: O(N)
+N: sの長さ
+ +```python +class Solution: + def isValid(self, s: str) -> bool: + open_to_close_bracket = {"(": ")", "{": "}", "[": "]"} + unclosed_open_brackets = [] + + for bracket in s: + if bracket in open_to_close_bracket: + unclosed_open_brackets.append(bracket) + continue + if len(unclosed_open_brackets) == 0 or open_to_close_bracket[unclosed_open_brackets[-1]] != bracket: + return False + unclosed_open_brackets.pop() + + return not unclosed_open_brackets +```