20.valid parentheses#6
Conversation
| class Solution { | ||
| public: | ||
| bool isValid(string s) { | ||
| unordered_map<char,char> map={{')','('},{'}','{'},{']','['}}; |
There was a problem hiding this comment.
= の両側にはスペースを空けることが多いと思います。
参考までにスタイルガイドへのリンクを共有いたします。
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={{')','('},{'}','{'},{']','['}}; |
There was a problem hiding this comment.
テンプレートパラメーター引数の , のあとには、スペースを空けることが多いと思います。
| class Solution { | ||
| public: | ||
| bool isValid(string s) { | ||
| unordered_map<char,char> map={{')','('},{'}','{'},{']','['}}; |
There was a problem hiding this comment.
,;'()[]{} が入り混じっていて、認識しづらく感じました。ペアごとに改行を入れると読み手にとって読みやすくなると思います。
unordered_map<char, char> map = {
{')', '('},
{'}', '{'},
{']', '['}
};| class Solution { | ||
| public: | ||
| bool isValid(string s) { | ||
| unordered_map<char,char> map={{')','('},{'}','{'},{']','['}}; |
There was a problem hiding this comment.
変数名にデータ構造名をつけても、読み手にとって情報があまりないように思いました。中にどのような値が格納されるかを表したほうが、読み手にとって有益だと思います。特に、 unordered_map/map 型の変数名は (キー)_to_(値) の形になっているのをよく見かけます。 close_to_open はいかがでしょうか?
There was a problem hiding this comment.
コメントありがとうございます。
コードの不備も指摘していただきありがたいです。
ハッシュマップにはそのような書き方があるのですね、参考にさせていただきます。
| public: | ||
| bool isValid(string s) { | ||
| unordered_map<char,char> map={{')','('},{'}','{'},{']','['}}; | ||
| stack<char>st; |
There was a problem hiding this comment.
ハッシュセットがclose_to_openであればスタックもclose_bracketsで分かりやすいですね。
ありがとうございます。
| bool isValid(string s) { | ||
| unordered_map<char,char> map={{')','('},{'}','{'},{']','['}}; | ||
| stack<char>st; | ||
| for(char c:s){ |
There was a problem hiding this comment.
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){ |
There was a problem hiding this comment.
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){ |
There was a problem hiding this comment.
{ の手前にスペースが空いているところと空いていないところが混ざっているのが気になりました。空けるほうに統一したほうが良いと思います。
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()){ |
There was a problem hiding this comment.
== の両側にスペースを空けることが多いと思います。
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()){ |
There was a problem hiding this comment.
std::unordered_map::contains() を使用したほうがシンプルになると思います。
https://cpprefjp.github.io/reference/unordered_map/unordered_map/contains.html
if (map.contains(c)) {There was a problem hiding this comment.
ありがとうございます。
そちらのほうが短く済みますね。
This problem: https://leetcode.com/problems/valid-parentheses/description/
Next problem: https://leetcode.com/problems/reverse-linked-list/description/