diff --git a/20. Valid Parentheses.md b/20. Valid Parentheses.md new file mode 100644 index 0000000..afe8667 --- /dev/null +++ b/20. Valid Parentheses.md @@ -0,0 +1,127 @@ +URL: https://leetcode.com/problems/valid-parentheses/description/ + +# Step 1 + +- 実装時間: 5分 +- 時間計算量: O(n) +- 空間計算量: O(n) + +```python +class Solution: + def isValid(self, s: str) -> bool: + opening_parentheses = [] + for c in s: + if c in ['(', '{', '[']: + opening_parentheses.append(c) + continue + if len(opening_parentheses) == 0: + return False + if c == ')' and opening_parentheses[-1] != '(': + return False + if c == '}' and opening_parentheses[-1] != '{': + return False + if c == ']' and opening_parentheses[-1] != '[': + return False + opening_parentheses.pop() + return True +``` + +一度間違えた。中途半端に開きっぱなしのケースの考慮漏れ。 + +- エラーケースを確認して修正した。 + +```python +class Solution: + def isValid(self, s: str) -> bool: + opening_parentheses = [] + for c in s: + if c in ['(', '{', '[']: + opening_parentheses.append(c) + continue + if len(opening_parentheses) == 0: + return False + if c == ')' and opening_parentheses[-1] != '(': + return False + if c == '}' and opening_parentheses[-1] != '{': + return False + if c == ']' and opening_parentheses[-1] != '[': + return False + opening_parentheses.pop() + return len(opening_parentheses) == 0 +``` + +# Step 2 + +- ループ変数`c`について。 + - 今回は、ループ変数`i`と同じノリで`c`でいいやと思って使った。でも、ある程度の長さのループで使うし、主役なので名前をつけても良かった。 + - https://github.com/konnysh/arai60/pull/6/files#r1843303374 + - parenthesesの単数形はparenthesis。 + - イギリス英語になるが`bracket`という命名はすべてのカッコを包含できる。 + - シンプルに`char`とするパターン + - これは`c`でよさそう + +- map(dictionary)を使うやり方。 + - 括弧の開閉のペアを`open_to_close`というmapであらかじめ持っておく。 + - 拡張性がありそう。 + - https://github.com/konnysh/arai60/pull/6/files + +- 最後の`len(opening_parentheses) == 0`について。 + - > PEP-8 と Google Style Guide は strings, lists, tuples は implicit でということでしたね。 +趣味の範囲でしょう。大規模開発などでは周りを見て合わせましょう。 + - https://github.com/BumbuShoji/Leetcode/pull/7/files#r1814477339 + - なるべくimplicitにする。 + - とはいえ、戻り値の型がboolの関数で`return List`とするのがなんとなく好きじゃない。 + - 趣味の範囲ということで、`len(opening_parentheses) == 0`を選ぶ。 + +- stringにシングルクオートを使うか、ダブルクオートを使うか。 + - どっちでもいいけど一貫性をもつ。 + - https://google.github.io/styleguide/pyguide.html#310-strings + - https://peps.python.org/pep-0008/#string-quotes + - 感覚的にC言語のcharと同じで、1文字だとシングルにしたい。 + +```python +class Solution: + def isValid(self, s: str) -> bool: + open_to_close = { + '(': ')', + '{': '}', + '[': ']' + } + opening_parentheses = [] + for parenthesis in s: + if parenthesis in open_to_close: + opening_parentheses.append(parenthesis) + continue + if not opening_parentheses: + return False + if parenthesis != open_to_close[opening_parentheses[-1]]: + return False + opening_parentheses.pop() + return len(opening_parentheses) == 0 +``` + +# Step 3 + +- 時間計算量: O(n) +- 空間計算量: O(n) + +```python +class Solution: + def isValid(self, s: str) -> bool: + open_to_close = { + '(': ')', + '{': '}', + '[': ']' + } + opening_parentheses = [] + for parenthesis in s: + if parenthesis in open_to_close: + opening_parentheses.append(parenthesis) + continue + if not opening_parentheses: + return False + if parenthesis != open_to_close[opening_parentheses[-1]]: + return False + opening_parentheses.pop() + return len(opening_parentheses) == 0 +```