diff --git a/20.ValidParentheses/memo.md b/20.ValidParentheses/memo.md new file mode 100644 index 0000000..0908e61 --- /dev/null +++ b/20.ValidParentheses/memo.md @@ -0,0 +1,45 @@ + + +## ステップ1 +vectorかStack (今回からのジャンル)を使って一文字ずつ確認すれば解けそう +submit後のエラーより + +2文字以下の場合のエラーハンドリング追加(マジックナンバーは2にしました) +{[]}の場合を考慮 +一文字めが)]}のいずれかの場合 +問題文を読んで自分の中で整理する前にコードを書き始めている。 + +時間計算量 +forループは入力の大きさに影響されるので最悪O(n) + +空間計算量 +全て開始ブラケットの場合、最悪O(n) + +## ステップ2 +stackについてきちんと学んでみた +https://cpprefjp.github.io/reference/stack/stack.html + +見た解法(同じくスタックを使う) +https://leetcode.com/problems/valid-parentheses/solutions/3399077/easy-solutions-in-java-python-and-c-look-at-once-with-exaplanation/ + +条件を整理してまとめるとbrackets.pop();を一度の記載で書くことができる。 +"]" 最初にいずれかの開始ブラケットが来てれば、stackは空ではない。 +  空かどうかの判断は1番はじめにしないと、空のスタックに対してPOPしてしまう。 +"" 最初から空の文字列が渡された場合もエラーで弾く +"[}" if文で愚直に判断 + +## 他の解法 +やっていることはStackと同じ +https://leetcode.com/problems/valid-parentheses/solutions/9478/no-stack-o-1-space-complexity-o-n-time-complexity-solution-in-c/ + +## Discordなど +意味のある変数名を持たせる +https://github.com/kzhra/Grind41/pull/2 + +講師陣はチョムスキー階層、タイプ-2、プッシュダウンオートマトンを連想する +https://discord.com/channels/1084280443945353267/1201211204547383386/1202541275115425822 + +それぞれ初耳でした。オートマトンに種類があったのですね。 +参照:https://ja.wikipedia.org/wiki/%E3%83%81%E3%83%A7%E3%83%A0%E3%82%B9%E3%82%AD%E3%83%BC%E9%9A%8E%E5%B1%A4 + +https://ja.wikipedia.org/wiki/%E3%83%97%E3%83%83%E3%82%B7%E3%83%A5%E3%83%80%E3%82%A6%E3%83%B3%E3%83%BB%E3%82%AA%E3%83%BC%E3%83%88%E3%83%9E%E3%83%88%E3%83%B3 diff --git a/20.ValidParentheses/memo_en.md b/20.ValidParentheses/memo_en.md new file mode 100644 index 0000000..a32c8fd --- /dev/null +++ b/20.ValidParentheses/memo_en.md @@ -0,0 +1,37 @@ + + +## Discordなど +意味のある変数名を持たせる +https://github.com/kzhra/Grind41/pull/2 + +講師陣はチョムスキー階層、タイプ-2、プッシュダウンオートマトンを連想する +https://discord.com/channels/1084280443945353267/1201211204547383386/1202541275115425822 + +それぞれ初耳でした。オートマトンに種類があったのですね。 +参照:https://ja.wikipedia.org/wiki/%E3%83%81%E3%83%A7%E3%83%A0%E3%82%B9%E3%82%AD%E3%83%BC%E9%9A%8E%E5%B1%A4 + +https://ja.wikipedia.org/wiki/%E3%83%97%E3%83%83%E3%82%B7%E3%83%A5%E3%83%80%E3%82%A6%E3%83%B3%E3%83%BB%E3%82%AA%E3%83%BC%E3%83%88%E3%83%9E%E3%83%88%E3%83%B3 + +## tips +・In the for loop, using the primitive char type is desire because char is small or 1 byte +but the reference is 4 ~ 8 byte based on the system architecture. + +・After checking the parirs are validated, the stack should be checked whether it is empty or not. + +・Instead using the Stack, the vector and string can be used to store the brackets status. + +・Instead usings[i] == '(', it is more readable to use map for storing the open and close brackets. + For instance, initializer can be used. + +・Conditions in the if statements should be alinged following google guide. +https://google.github.io/styleguide/cppguide.html#Boolean_Expressions + +・Instead of using else statement, the early reaturn can be used for a readability. + +・In the LeetCode the std:: doesn't need to be used. In the company, it is required to follow team's rule. + + + + + + diff --git a/20.ValidParentheses/step1.cpp b/20.ValidParentheses/step1.cpp new file mode 100644 index 0000000..9ef67ba --- /dev/null +++ b/20.ValidParentheses/step1.cpp @@ -0,0 +1,32 @@ +#include +#include +class Solution { +public: + bool isValid(string s) { + std::stack brackets; + if (s.length() < 2) { + return false; + } + + for (int i = 0; i < s.length(); i++) { + if (brackets.empty() && (s[i] == ')' || s[i] == '}' || s[i] == ']')) { + return false; + } else if (s[i] == '(' || s[i] == '{' || s[i] == '[' ) { + brackets.push(s[i]); + } else if (s[i] == ')' && brackets.top() == '(') { + brackets.pop(); + } else if (s[i] == '}' && brackets.top() == '{') { + brackets.pop(); + } else if (s[i] == ']' && brackets.top() == '[') { + brackets.pop(); + } else { + return false; + } + } + + if (brackets.size() != 0) { + return false; + } + return true; + } +}; diff --git a/20.ValidParentheses/step2.cpp b/20.ValidParentheses/step2.cpp new file mode 100644 index 0000000..1f84d9c --- /dev/null +++ b/20.ValidParentheses/step2.cpp @@ -0,0 +1,24 @@ +#include +#include +class Solution { +public: + bool isValid(string s) { + std::stack open_brackets; + + for (int i = 0; i < s.length(); i++) { + if (s[i] == '(' || s[i] == '{' || s[i] == '[' ) { + open_brackets.push(s[i]); + } else if ( + open_brackets.empty() || + s[i] == ')' && open_brackets.top() != '(' || + s[i] == '}' && open_brackets.top() != '{' || + s[i] == ']' && open_brackets.top() != '[' + ) { + return false; + } else { + open_brackets.pop(); + } + } + return open_brackets.empty(); + } +}; diff --git a/20.ValidParentheses/step3.cpp b/20.ValidParentheses/step3.cpp new file mode 100644 index 0000000..2da12c9 --- /dev/null +++ b/20.ValidParentheses/step3.cpp @@ -0,0 +1,26 @@ +#include +#include + +class Solution { +public: + bool isValid(string s) { + std::stack open_brackets; + + for (int i = 0; i < s.length(); i++) { + if (s[i] == '(' || s[i] == '[' || s[i] == '{') { + open_brackets.push(s[i]); + } else if ( + open_brackets.empty() || + (s[i] == ')' && open_brackets.top() != '(') || + (s[i] == ']' && open_brackets.top() != '[') || + (s[i] == '}' && open_brackets.top() != '{') + ) { + return false; + } else { + open_brackets.pop(); + } + } + + return open_brackets.empty(); + } +}; diff --git a/20.ValidParentheses/step4.cpp b/20.ValidParentheses/step4.cpp new file mode 100644 index 0000000..739f4c6 --- /dev/null +++ b/20.ValidParentheses/step4.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +class Solution { +public: + bool isValid(string s) { + std::stack open_brackets; + std::map brackets_pairs = { + {'(', ')'}, + {'{', '}'}, + {'[', ']'}, + }; + + for (int i = 0; i < s.length(); i++) { + if (brackets_pairs.contains(s[i])) { + open_brackets.push(s[i]); + continue; + } + + if (open_brackets.empty() || + brackets_pairs[open_brackets.top()] != s[i]) { + return false; + } + open_brackets.pop(); + } + return open_brackets.empty(); + } +}; diff --git a/20.ValidParentheses/step5.cpp b/20.ValidParentheses/step5.cpp new file mode 100644 index 0000000..1c93bc3 --- /dev/null +++ b/20.ValidParentheses/step5.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + bool isValid(string s) { + map brackets_pairs = {{'(', ')'}, {'{', '}'}, {'[', ']'} }; + + stack open_brackets; + for (auto bracket : s) { + if (bracket == '(' || bracket == '{' || bracket == '[') { + open_brackets.emplace(bracket); + continue; + } + if (open_brackets.empty() || bracket != brackets_pairs[open_brackets.top()]) { + return false; + } + open_brackets.pop(); + } + return open_brackets.empty(); + } +};