Skip to content

Arai60/20#8

Open
liruly wants to merge 2 commits intomainfrom
Arai60/20
Open

Arai60/20#8
liruly wants to merge 2 commits intomainfrom
Arai60/20

Conversation

@liruly
Copy link
Copy Markdown
Owner

@liruly liruly commented Jan 12, 2026

20. Valid Parentheses

Copy link
Copy Markdown

@05ryt31 05ryt31 left a comment

Choose a reason for hiding this comment

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

普段C++を書かない自分でもとても読みやすいコード、memoだと思いました。

Copy link
Copy Markdown

@xbam326 xbam326 left a comment

Choose a reason for hiding this comment

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

step2で他の人のコードを読んで考えたことや、良かった点をまとめるとより学びが得られるのではないかと思いました。

Comment on lines +46 to +52
if (match.count(c)) {
st.push(c);
continue;
} else if (!st.empty() && match[st.top()] == c ) {
st.pop();
continue;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

resumit30minutes/leetcode-arai60-practice#7 (comment)
問題上起こりえないですが、こちらのディスカッションでは意図しない文字が入ってきた場合はどうするか?ということが議論されておりました。
今回のコードではFalseを返すと思っており、私もFalseを返すorエラーを吐くのが良いのでは?と思っているのですが、そうではないと考える方もおり、多角的な意見が学べたので紹介します。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
第一義的にはerrを返して終了すべきだと考えます(今回の場合はfalse返すしかないですが)。
PythonもC++も、普通にアクセスしてしまうと新しくkeyが作成されてしまうと認識しているので、事前にその要素があるか、cppなら.count()、pythonなら"key" in D等でチェックするのが安全な設計をするなら望ましいとも思います。

if (match.count(c)) {
st.push(c);
continue;
} else if (!st.empty() && match[st.top()] == c ) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私はここ else 消します。上が continue なので。
また、条件はひっくり返して、return false; を中にいれます。
いずれも趣味の範囲です。

まあ、あとは、この問題はプッシュダウンオートマトンを連想するので、一応それは確認してみてください。たぶん、完走した人とかを調べると誰かよくまとめているでしょう。「in: レビュー依頼 55」あたりで検索すると完走した人たちが出てきます。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

continueの後のelseを残すかは悩みどころでした。可読性の面からつけたほうがいいかなと思っていましたが、むしろコード量が増えてしまうので避けても良さそうです。個人的にもシンプルなほうが好きなので次回からはそうしようと思います。
正常系・異常系のどちらかをifブロックに入れるなら異常系とも思います。こちらも意識します。

プッシュダウンオートマトン(というよりオートマトンという概念自体)については存じ上げなかったのですが、幸いBNFに基づいた再帰下降構文解析は書いたことがあるので、この問題でも実装してみます。

Copy link
Copy Markdown

@dxxsxsxkx dxxsxsxkx left a comment

Choose a reason for hiding this comment

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

わかりやすかったです👌

bool isValid(std::string s) {
std::stack<char> st;

std::unordered_map<char, char> match = {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

現状のままでもそんなにわかりづらくはないですが、自分であれば matched_parentheses ないし open_to_close とするかなと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

確かに、結局何が入っているのか変数名から理解できないですね。ご指摘ありがとうございます。

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.

5 participants