Skip to content

Conversation

Ryotaro25
Copy link
Owner

問題へのリンク
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内に各ステップで感じたことを追記します。

std::stack<char> open_brackets;

for (int i = 0; i < s.length(); i++) {
if (s[i] == '(' || s[i] == '{' || s[i] == '[' ) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

条件式がやや複雑に見えました。 ( と ) の対応を map に入れて、 map をさんしょうするなどして、シンプルにできますか?

Copy link
Owner Author

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

for (int i = 0; i < s.length(); i++) {
if (s[i] == '(' || s[i] == '{' || s[i] == '[' ) {
open_brackets.push(s[i]);
} else if (
Copy link

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

Copy link
Owner Author

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]);

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 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上のif節でcontinueするとここでelseが不要になります

Copy link
Owner Author

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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LeetCode の場合は std:: を付けなくても良いようです。実際のお仕事の場で付けるべきかどうかは、コーディング規約に従うのがよいと思います。

public:
bool isValid(string s) {
std::stack<char> open_brackets;
std::map<char, char> brackets_pairs;
Copy link

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 = {
    {'(', ')'},
    {'{', '}'},
    {'[', ']'},
};

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nodchip
こちらの方がぱっと見でペアになっていて良いです。下記にて修正したものをPushしました。
e69c054

if (brackets_pairs.contains(s[i])) {
open_brackets.push(s[i]);
continue;
} else if (open_brackets.empty() ||
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分なら continue したあとは else 節にせず、独立した if 文にすると思います。理由は、そのほうが処理の切れ目が見た目で分かりやすく、コードが読みやすくなると思うからです。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ryotaro25
continueで処理が次のイテレーションに移るのでこちらの方が読みやすいですね🙇
e69c054

@Ryotaro25 Ryotaro25 merged commit e5dfeff into main Oct 8, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants