Skip to content

20.valid parentheses#6

Open
nicah4o wants to merge 1 commit into
mainfrom
20.valid-parentheses
Open

20.valid parentheses#6
nicah4o wants to merge 1 commit into
mainfrom
20.valid-parentheses

Conversation

@nicah4o
Copy link
Copy Markdown
Owner

@nicah4o nicah4o commented May 7, 2026

class Solution {
public:
bool isValid(string s) {
unordered_map<char,char> map={{')','('},{'}','{'},{']','['}};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

= の両側にはスペースを空けることが多いと思います。

参考までにスタイルガイドへのリンクを共有いたします。

https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace

// Assignment operators always have spaces around them.
x = 0;

なお、このスタイルガイドは“唯一の正解”というわけではなく、数あるガイドラインの一つに過ぎません。チームによって重視される書き方や慣習も異なります。そのため、ご自身の中に基準を持ちつつも、最終的にはチームの一般的な書き方に合わせることをお勧めします。

class Solution {
public:
bool isValid(string s) {
unordered_map<char,char> map={{')','('},{'}','{'},{']','['}};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

テンプレートパラメーター引数の , のあとには、スペースを空けることが多いと思います。

class Solution {
public:
bool isValid(string s) {
unordered_map<char,char> map={{')','('},{'}','{'},{']','['}};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

,;'()[]{} が入り混じっていて、認識しづらく感じました。ペアごとに改行を入れると読み手にとって読みやすくなると思います。

unordered_map<char, char> map = {
    {')', '('},
    {'}', '{'},
    {']', '['}
};

class Solution {
public:
bool isValid(string s) {
unordered_map<char,char> map={{')','('},{'}','{'},{']','['}};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

変数名にデータ構造名をつけても、読み手にとって情報があまりないように思いました。中にどのような値が格納されるかを表したほうが、読み手にとって有益だと思います。特に、 unordered_map/map 型の変数名は (キー)_to_(値) の形になっているのをよく見かけます。 close_to_open はいかがでしょうか?

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.

コメントありがとうございます。
コードの不備も指摘していただきありがたいです。
ハッシュマップにはそのような書き方があるのですね、参考にさせていただきます。

public:
bool isValid(string s) {
unordered_map<char,char> map={{')','('},{'}','{'},{']','['}};
stack<char>st;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらは close_brackets はいかがでしょうか?

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.

ハッシュセットがclose_to_openであればスタックもclose_bracketsで分かりやすいですね。
ありがとうございます。

bool isValid(string s) {
unordered_map<char,char> map={{')','('},{'}','{'},{']','['}};
stack<char>st;
for(char c:s){
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

if/for/while のあとにはスペースを空けることが多いと思います。

https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace

if (b) { // Space after the keyword in conditions and loops.

bool isValid(string s) {
unordered_map<char,char> map={{')','('},{'}','{'},{']','['}};
stack<char>st;
for(char c:s){
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ranged for 文の : の前後にはスペースを空けることが多いと思います。

https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace

// Range-based for loops always have a space before and after the colon.
for (auto x : counts) {

bool isValid(string s) {
unordered_map<char,char> map={{')','('},{'}','{'},{']','['}};
stack<char>st;
for(char c:s){
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

{ の手前にスペースが空いているところと空いていないところが混ざっているのが気になりました。空けるほうに統一したほうが良いと思います。

https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace

void f(bool b) { // Open braces should always have a space before them.

unordered_map<char,char> map={{')','('},{'}','{'},{']','['}};
stack<char>st;
for(char c:s){
if(map.find(c)==map.end()){
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

== の両側にスペースを空けることが多いと思います。

https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace

// Other binary operators usually have spaces around them

unordered_map<char,char> map={{')','('},{'}','{'},{']','['}};
stack<char>st;
for(char c:s){
if(map.find(c)==map.end()){
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

std::unordered_map::contains() を使用したほうがシンプルになると思います。
https://cpprefjp.github.io/reference/unordered_map/unordered_map/contains.html

if (map.contains(c)) {

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.

2 participants