-
Notifications
You must be signed in to change notification settings - Fork 0
20. Valid Parentheses #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
||
|
||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <string> | ||
#include <stack> | ||
class Solution { | ||
public: | ||
bool isValid(string s) { | ||
std::stack<char> 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; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <string> | ||
#include <stack> | ||
class Solution { | ||
public: | ||
bool isValid(string s) { | ||
std::stack<char> open_brackets; | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
if (s[i] == '(' || s[i] == '{' || s[i] == '[' ) { | ||
open_brackets.push(s[i]); | ||
} else if ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if 文の条件式の line break の位置を Google C++ Style Guide に合わせるとよいと思います。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 資料有難うございます。今回以降気をつけます。 |
||
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(); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <stack> | ||
#include <string> | ||
|
||
class Solution { | ||
public: | ||
bool isValid(string s) { | ||
std::stack<char> open_brackets; | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
if (s[i] == '(' || s[i] == '[' || s[i] == '{') { | ||
open_brackets.push(s[i]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここでcontinueしちゃってもいいかもしれませんね |
||
} else if ( | ||
open_brackets.empty() || | ||
(s[i] == ')' && open_brackets.top() != '(') || | ||
(s[i] == ']' && open_brackets.top() != '[') || | ||
(s[i] == '}' && open_brackets.top() != '{') | ||
) { | ||
return false; | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 上のif節でcontinueするとここでelseが不要になります There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sakupan102 |
||
open_brackets.pop(); | ||
} | ||
} | ||
|
||
return open_brackets.empty(); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <string> | ||
#include <stack> | ||
#include <map> | ||
class Solution { | ||
public: | ||
bool isValid(string s) { | ||
std::stack<char> open_brackets; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LeetCode の場合は std:: を付けなくても良いようです。実際のお仕事の場で付けるべきかどうかは、コーディング規約に従うのがよいと思います。 |
||
std::map<char, char> 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(); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution { | ||
public: | ||
bool isValid(string s) { | ||
map<char, char> brackets_pairs = {{'(', ')'}, {'{', '}'}, {'[', ']'} }; | ||
|
||
stack<char> 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(); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
条件式がやや複雑に見えました。 ( と ) の対応を map に入れて、 map をさんしょうするなどして、シンプルにできますか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nodchip
step4.cppとして指摘箇所踏まえたファイルを作成しました。
ifとelse ifの判定にmapを使うことでコードを短く出来ました。
9ec3a48