From 65496d0cd426cba00d919c208e77b586bd8d9a01 Mon Sep 17 00:00:00 2001 From: kazukiii Date: Mon, 10 Jun 2024 16:14:38 -0700 Subject: [PATCH 1/2] =?UTF-8?q?step1,=20step2,=20step3=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arai60/valid-parentheses/README.md | 22 ++++++++++++++++++++++ arai60/valid-parentheses/step1.py | 22 ++++++++++++++++++++++ arai60/valid-parentheses/step2.py | 20 ++++++++++++++++++++ arai60/valid-parentheses/step3.py | 20 ++++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 arai60/valid-parentheses/README.md create mode 100644 arai60/valid-parentheses/step1.py create mode 100644 arai60/valid-parentheses/step2.py create mode 100644 arai60/valid-parentheses/step3.py diff --git a/arai60/valid-parentheses/README.md b/arai60/valid-parentheses/README.md new file mode 100644 index 0000000..08e51e8 --- /dev/null +++ b/arai60/valid-parentheses/README.md @@ -0,0 +1,22 @@ +## 考察 +- 過去に解いたことあり +- 方針 + - stackを使って対応を見ていく + - 前から文字を見ていって、open bracketが来たらstackに積む + - close bracketが来たら、stackから要素をpopして対応をチェックする(stackが空じゃないかだけ注意) + - 最後にstackが空になっていたらOK + - time: O(n), space: O(n) +- あとは実装 + +## Step1 +- 上のアルゴリズムを実装 +- time: O(n), space: O(n) + +## Step2 +- 手続き的にbracketの対応関係を判定していた箇所を、辞書を使って宣言的な記述に変更 + - 結果、余計なif文がかなり減った + +## Step3 +- 1回目: 1m51s +- 2回目: 1m53s +- 3回目: 1m28s diff --git a/arai60/valid-parentheses/step1.py b/arai60/valid-parentheses/step1.py new file mode 100644 index 0000000..b99671b --- /dev/null +++ b/arai60/valid-parentheses/step1.py @@ -0,0 +1,22 @@ +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + for c in s: + if c in ['(', '{', '[']: + stack.append(c) + else: + if not stack: + return False + + open_bracket = stack.pop() + if c == ')': + if not open_bracket == '(': + return False + elif c == '}': + if not open_bracket == '{': + return False + else: + if not open_bracket == '[': + return False + + return not stack diff --git a/arai60/valid-parentheses/step2.py b/arai60/valid-parentheses/step2.py new file mode 100644 index 0000000..e5787a1 --- /dev/null +++ b/arai60/valid-parentheses/step2.py @@ -0,0 +1,20 @@ +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + bracket_pairs = { + '(': ')', + '{': '}', + '[': ']', + } + for c in s: + if c in bracket_pairs: + stack.append(c) + else: + if not stack: + return False + + open_bracket = stack.pop() + if not bracket_pairs[open_bracket] == c: + return False + + return not stack diff --git a/arai60/valid-parentheses/step3.py b/arai60/valid-parentheses/step3.py new file mode 100644 index 0000000..e5787a1 --- /dev/null +++ b/arai60/valid-parentheses/step3.py @@ -0,0 +1,20 @@ +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + bracket_pairs = { + '(': ')', + '{': '}', + '[': ']', + } + for c in s: + if c in bracket_pairs: + stack.append(c) + else: + if not stack: + return False + + open_bracket = stack.pop() + if not bracket_pairs[open_bracket] == c: + return False + + return not stack From a9ff7d4c80acda85ac04d41d2333121b257337b3 Mon Sep 17 00:00:00 2001 From: kazukiii Date: Tue, 11 Jun 2024 13:54:47 -0700 Subject: [PATCH 2/2] =?UTF-8?q?step4=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arai60/valid-parentheses/README.md | 11 +++++++++++ arai60/valid-parentheses/step4.py | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 arai60/valid-parentheses/step4.py diff --git a/arai60/valid-parentheses/README.md b/arai60/valid-parentheses/README.md index 08e51e8..7009261 100644 --- a/arai60/valid-parentheses/README.md +++ b/arai60/valid-parentheses/README.md @@ -20,3 +20,14 @@ - 1回目: 1m51s - 2回目: 1m53s - 3回目: 1m28s + +## Step4 +- レビューを元にコードを修正 +- 変数名の見直し + - stack -> bracket_stack + - 何を入れるstackなのかを明確に + - bracket_pairs -> open_to_close + - 例えば、open_to_close[open_bracket] という記述を見たときにclose_bracketが返ってくることが一目でわかる + - より具体的になって、わかりやすくなった感覚 +- `if not open_to_close[open_bracket] == c:` と書いていたところを `if open_to_close[open_bracket] != c:`に +- early continueしてネストを浅く diff --git a/arai60/valid-parentheses/step4.py b/arai60/valid-parentheses/step4.py new file mode 100644 index 0000000..4fa6a73 --- /dev/null +++ b/arai60/valid-parentheses/step4.py @@ -0,0 +1,21 @@ +class Solution: + def isValid(self, s: str) -> bool: + bracket_stack = [] + open_to_close = { + '(': ')', + '{': '}', + '[': ']', + } + for c in s: + if c in open_to_close: + bracket_stack.append(c) + continue + + if not bracket_stack: + return False + + open_bracket = bracket_stack.pop() + if open_to_close[open_bracket] != c: + return False + + return not bracket_stack