-
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
Conversation
std::stack<char> open_brackets; | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
if (s[i] == '(' || s[i] == '{' || s[i] == '[' ) { |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
if 文の条件式の line break の位置を Google C++ Style Guide に合わせるとよいと思います。
https://google.github.io/styleguide/cppguide.html#Boolean_Expressions
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.
資料有難うございます。今回以降気をつけます。
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
ここでcontinueしちゃってもいいかもしれませんね
(s[i] == '}' && open_brackets.top() != '{') | ||
) { | ||
return false; | ||
} else { |
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.
上のif節でcontinueするとここでelseが不要になります
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.
@sakupan102
レビュー有難うございます。
step4.cppとして指摘箇所踏まえたファイルを作成しました。
else を削除することが出来ました。もっとシンプルに書けるように意識します。
9ec3a48
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 comment
The reason will be displayed to describe this comment to others. Learn more.
LeetCode の場合は std:: を付けなくても良いようです。実際のお仕事の場で付けるべきかどうかは、コーディング規約に従うのがよいと思います。
20.ValidParentheses/step4.cpp
Outdated
public: | ||
bool isValid(string s) { | ||
std::stack<char> open_brackets; | ||
std::map<char, char> brackets_pairs; |
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.
initializer を使って初期化したほうがシンプルだと思います。
map<char, char> brackets_pairs = {
{'(', ')'},
{'{', '}'},
{'[', ']'},
};
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.
20.ValidParentheses/step4.cpp
Outdated
if (brackets_pairs.contains(s[i])) { | ||
open_brackets.push(s[i]); | ||
continue; | ||
} else if (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.
自分なら continue したあとは else 節にせず、独立した if 文にすると思います。理由は、そのほうが処理の切れ目が見た目で分かりやすく、コードが読みやすくなると思うからです。
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.
@Ryotaro25
continueで処理が次のイテレーションに移るのでこちらの方が読みやすいですね🙇
e69c054
問題へのリンク
https://leetcode.com/problems/valid-parentheses/description/
問題文(プレミアムの場合)
備考
次に解く問題の予告
206. Reverse Linked List
https://leetcode.com/problems/reverse-linked-list/description/
フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cppとmemo.mdとなります。
memo.md内に各ステップで感じたことを追記します。