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.
条件式がやや複雑に見えました。 ( と ) の対応を map に入れて、 map をさんしょうするなどして、シンプルにできますか?
| 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.
if 文の条件式の line break の位置を Google C++ Style Guide に合わせるとよいと思います。
https://google.github.io/styleguide/cppguide.html#Boolean_Expressions
|
|
||
| for (int i = 0; i < s.length(); i++) { | ||
| if (s[i] == '(' || s[i] == '[' || s[i] == '{') { | ||
| open_brackets.push(s[i]); |
| (s[i] == '}' && open_brackets.top() != '{') | ||
| ) { | ||
| return false; | ||
| } else { |
There was a problem hiding this comment.
@sakupan102
レビュー有難うございます。
step4.cppとして指摘箇所踏まえたファイルを作成しました。
else を削除することが出来ました。もっとシンプルに書けるように意識します。
9ec3a48
| class Solution { | ||
| public: | ||
| bool isValid(string s) { | ||
| std::stack<char> open_brackets; |
There was a problem hiding this comment.
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.
initializer を使って初期化したほうがシンプルだと思います。
map<char, char> brackets_pairs = {
{'(', ')'},
{'{', '}'},
{'[', ']'},
};
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.
自分なら continue したあとは else 節にせず、独立した if 文にすると思います。理由は、そのほうが処理の切れ目が見た目で分かりやすく、コードが読みやすくなると思うからです。
There was a problem hiding this comment.
@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内に各ステップで感じたことを追記します。