From a5ebcddbb7f7edd9211bd290becb4fc14eea07c8 Mon Sep 17 00:00:00 2001 From: liruly <141731612+liruly@users.noreply.github.com> Date: Mon, 12 Jan 2026 23:36:24 +0900 Subject: [PATCH 1/2] Add: 20 --- 20_ValidParentheses/20_ValidParentheses.cpp | 56 ++++++++++++ 20_ValidParentheses/20_ValidParentheses.md | 95 +++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 20_ValidParentheses/20_ValidParentheses.cpp create mode 100644 20_ValidParentheses/20_ValidParentheses.md diff --git a/20_ValidParentheses/20_ValidParentheses.cpp b/20_ValidParentheses/20_ValidParentheses.cpp new file mode 100644 index 0000000..3c560f5 --- /dev/null +++ b/20_ValidParentheses/20_ValidParentheses.cpp @@ -0,0 +1,56 @@ +// data_structures + +// Step1 +#include +#include + +class Solution { +public: + bool isValid(std::string s) { + std::stack st; + + for (char c : s) { + if (c == '(' || c == '{' || c == '[') { + st.push(c); + continue; + } else if ((c == ')' || c == '}' || c == ']') && !st.empty()) { + const char top = st.top(); + if ((top == '(' && c == ')') || (top == '{' && c == '}') || (top == '[' && c == ']')) { + st.pop(); + continue; + } + } + return false; + } + return st.empty(); + } +}; + +// Step2 +#include +#include +#include + +class Solution { +public: + bool isValid(std::string s) { + std::stack st; + + std::unordered_map match = { + {'(', ')'}, + {'{', '}'}, + {'[', ']'} + }; + + for (char c : s) { + if (match.count(c)) { + st.push(c); + } else if (!st.empty() && match[st.top()] == c ) { + st.pop(); + } else { + return false; + } + } + return st.empty(); + } +}; diff --git a/20_ValidParentheses/20_ValidParentheses.md b/20_ValidParentheses/20_ValidParentheses.md new file mode 100644 index 0000000..b74fdfa --- /dev/null +++ b/20_ValidParentheses/20_ValidParentheses.md @@ -0,0 +1,95 @@ +# 20. Valid Parentheses +- 問題文: [20. Valid Parentheses](https://leetcode.com/problems/valid-parentheses/description/) +- 使用言語: C++ +- 次に解く問題: [206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/description/) + +## 知識セット +### 知っていたこと +- stackというデータ構造 + +### 今回調べたこと +- std::stackの使い方 + - メソッド: + - push: スタックに追加 + - top: 一番上の値を返す + - pop: 一番上の値を削除(返り値は無し) + - empty: 空ならtrue, 入っていたらfalse + - size: sizeを返す +- 範囲for文の使い方 +- std::unordered_mapの使い方 + +## Step1 +- 所要時間: 10min. +- 方針: + - 愚直にstackに積んでいって、対応しているかチェック +- コード +```cpp +#include +#include + +class Solution { +public: + bool isValid(std::string s) { + std::stack st; + + for (char c : s) { + if (c == '(' || c == '{' || c == '[') { + st.push(c); + continue; + } else if ((c == ')' || c == '}' || c == ']') && !st.empty()) { + const char top = st.top(); + if ((top == '(' && c == ')') || (top == '{' && c == '}') || (top == '[' && c == ']')) { + st.pop(); + continue; + } + } + return false; + } + return st.empty(); + } +}; +``` +- 計算量 + - 時間計算量: O(N) + - 空間計算量: O(N) + +## Step2 +- 所要時間: 20min. +- 方針: + - 辞書を使って条件文を簡単に + - ネストを減らす +- コード +```cpp +#include +#include +#include + +class Solution { +public: + bool isValid(std::string s) { + std::stack st; + + std::unordered_map match = { + {'(', ')'}, + {'{', '}'}, + {'[', ']'} + }; + + for (char c : s) { + if (match.count(c)) { + st.push(c); + } else if (!st.empty() && match[st.top()] == c ) { + st.pop(); + } else { + return false; + } + } + return st.empty(); + } +}; +``` +- 計算量 + - 時間計算量: O(N) + - dictの分は高々定数倍 + - 空間計算量: O(N) + - dictの文が追加されたとて \ No newline at end of file From 83dbdd2793de6b6f2389eafb7055450856a831d9 Mon Sep 17 00:00:00 2001 From: liruly <141731612+liruly@users.noreply.github.com> Date: Mon, 12 Jan 2026 23:39:13 +0900 Subject: [PATCH 2/2] refactor: edit loop adressing --- 20_ValidParentheses/20_ValidParentheses.cpp | 5 +++-- 20_ValidParentheses/20_ValidParentheses.md | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/20_ValidParentheses/20_ValidParentheses.cpp b/20_ValidParentheses/20_ValidParentheses.cpp index 3c560f5..9290e61 100644 --- a/20_ValidParentheses/20_ValidParentheses.cpp +++ b/20_ValidParentheses/20_ValidParentheses.cpp @@ -45,11 +45,12 @@ class Solution { for (char c : s) { if (match.count(c)) { st.push(c); + continue; } else if (!st.empty() && match[st.top()] == c ) { st.pop(); - } else { - return false; + continue; } + return false; } return st.empty(); } diff --git a/20_ValidParentheses/20_ValidParentheses.md b/20_ValidParentheses/20_ValidParentheses.md index b74fdfa..f8eedca 100644 --- a/20_ValidParentheses/20_ValidParentheses.md +++ b/20_ValidParentheses/20_ValidParentheses.md @@ -78,11 +78,12 @@ public: for (char c : s) { if (match.count(c)) { st.push(c); + continue; } else if (!st.empty() && match[st.top()] == c ) { st.pop(); - } else { - return false; + continue; } + return false; } return st.empty(); }