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 +``` 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..d71e924 --- /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,26 @@ + +変数名に関して: +今回、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 len(bracket_stack) == 0: + return False + + if bracket_dict[bracket_stack[-1]] != bracket: + return False + + bracket_stack.pop() + + return not bracket_stack +```