Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions 20.ValidParentheses/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@


## ステップ1
vectorかStack (今回からのジャンル)を使って一文字ずつ確認すれば解けそう
submit後のエラーより

2文字以下の場合のエラーハンドリング追加(マジックナンバーは2にしました)
{[]}の場合を考慮
一文字めが)]}のいずれかの場合
問題文を読んで自分の中で整理する前にコードを書き始めている。

時間計算量
forループは入力の大きさに影響されるので最悪O(n)

空間計算量
全て開始ブラケットの場合、最悪O(n)

## ステップ2
stackについてきちんと学んでみた
https://cpprefjp.github.io/reference/stack/stack.html

見た解法(同じくスタックを使う)
https://leetcode.com/problems/valid-parentheses/solutions/3399077/easy-solutions-in-java-python-and-c-look-at-once-with-exaplanation/

条件を整理してまとめるとbrackets.pop();を一度の記載で書くことができる。
"]" 最初にいずれかの開始ブラケットが来てれば、stackは空ではない。
  空かどうかの判断は1番はじめにしないと、空のスタックに対してPOPしてしまう。
"" 最初から空の文字列が渡された場合もエラーで弾く
"[}" if文で愚直に判断

## 他の解法
やっていることはStackと同じ
https://leetcode.com/problems/valid-parentheses/solutions/9478/no-stack-o-1-space-complexity-o-n-time-complexity-solution-in-c/

## Discordなど
意味のある変数名を持たせる
https://github.com/kzhra/Grind41/pull/2

講師陣はチョムスキー階層、タイプ-2、プッシュダウンオートマトンを連想する
https://discord.com/channels/1084280443945353267/1201211204547383386/1202541275115425822

それぞれ初耳でした。オートマトンに種類があったのですね。
参照:https://ja.wikipedia.org/wiki/%E3%83%81%E3%83%A7%E3%83%A0%E3%82%B9%E3%82%AD%E3%83%BC%E9%9A%8E%E5%B1%A4

https://ja.wikipedia.org/wiki/%E3%83%97%E3%83%83%E3%82%B7%E3%83%A5%E3%83%80%E3%82%A6%E3%83%B3%E3%83%BB%E3%82%AA%E3%83%BC%E3%83%88%E3%83%9E%E3%83%88%E3%83%B3
37 changes: 37 additions & 0 deletions 20.ValidParentheses/memo_en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@


## Discordなど
意味のある変数名を持たせる
https://github.com/kzhra/Grind41/pull/2

講師陣はチョムスキー階層、タイプ-2、プッシュダウンオートマトンを連想する
https://discord.com/channels/1084280443945353267/1201211204547383386/1202541275115425822

それぞれ初耳でした。オートマトンに種類があったのですね。
参照:https://ja.wikipedia.org/wiki/%E3%83%81%E3%83%A7%E3%83%A0%E3%82%B9%E3%82%AD%E3%83%BC%E9%9A%8E%E5%B1%A4

https://ja.wikipedia.org/wiki/%E3%83%97%E3%83%83%E3%82%B7%E3%83%A5%E3%83%80%E3%82%A6%E3%83%B3%E3%83%BB%E3%82%AA%E3%83%BC%E3%83%88%E3%83%9E%E3%83%88%E3%83%B3

## tips
・In the for loop, using the primitive char type is desire because char is small or 1 byte
but the reference is 4 ~ 8 byte based on the system architecture.

・After checking the parirs are validated, the stack should be checked whether it is empty or not.

・Instead using the Stack, the vector and string can be used to store the brackets status.

・Instead usings[i] == '(', it is more readable to use map for storing the open and close brackets.
For instance, initializer can be used.

・Conditions in the if statements should be alinged following google guide.
https://google.github.io/styleguide/cppguide.html#Boolean_Expressions

・Instead of using else statement, the early reaturn can be used for a readability.

・In the LeetCode the std:: doesn't need to be used. In the company, it is required to follow team's rule.






32 changes: 32 additions & 0 deletions 20.ValidParentheses/step1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <string>
#include <stack>
class Solution {
public:
bool isValid(string s) {
std::stack<char> brackets;
if (s.length() < 2) {
return false;
}

for (int i = 0; i < s.length(); i++) {
if (brackets.empty() && (s[i] == ')' || s[i] == '}' || s[i] == ']')) {
return false;
} else if (s[i] == '(' || s[i] == '{' || s[i] == '[' ) {
brackets.push(s[i]);
} else if (s[i] == ')' && brackets.top() == '(') {
brackets.pop();
} else if (s[i] == '}' && brackets.top() == '{') {
brackets.pop();
} else if (s[i] == ']' && brackets.top() == '[') {
brackets.pop();
} else {
return false;
}
}

if (brackets.size() != 0) {
return false;
}
return true;
}
};
24 changes: 24 additions & 0 deletions 20.ValidParentheses/step2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <string>
#include <stack>
class Solution {
public:
bool isValid(string s) {
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

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.

資料有難うございます。今回以降気をつけます。

open_brackets.empty() ||
s[i] == ')' && open_brackets.top() != '(' ||
s[i] == '}' && open_brackets.top() != '{' ||
s[i] == ']' && open_brackets.top() != '['
) {
return false;
} else {
open_brackets.pop();
}
}
return open_brackets.empty();
}
};
26 changes: 26 additions & 0 deletions 20.ValidParentheses/step3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stack>
#include <string>

class Solution {
public:
bool isValid(string s) {
std::stack<char> open_brackets;

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しちゃってもいいかもしれませんね

} else if (
open_brackets.empty() ||
(s[i] == ')' && open_brackets.top() != '(') ||
(s[i] == ']' && open_brackets.top() != '[') ||
(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

open_brackets.pop();
}
}

return open_brackets.empty();
}
};
28 changes: 28 additions & 0 deletions 20.ValidParentheses/step4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <string>
#include <stack>
#include <map>
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:: を付けなくても良いようです。実際のお仕事の場で付けるべきかどうかは、コーディング規約に従うのがよいと思います。

std::map<char, char> brackets_pairs = {
{'(', ')'},
{'{', '}'},
{'[', ']'},
};

for (int i = 0; i < s.length(); i++) {
if (brackets_pairs.contains(s[i])) {
open_brackets.push(s[i]);
continue;
}

if (open_brackets.empty() ||
brackets_pairs[open_brackets.top()] != s[i]) {
return false;
}
open_brackets.pop();
}
return open_brackets.empty();
}
};
19 changes: 19 additions & 0 deletions 20.ValidParentheses/step5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
bool isValid(string s) {
map<char, char> brackets_pairs = {{'(', ')'}, {'{', '}'}, {'[', ']'} };

stack<char> open_brackets;
for (auto bracket : s) {
if (bracket == '(' || bracket == '{' || bracket == '[') {
open_brackets.emplace(bracket);
continue;
}
if (open_brackets.empty() || bracket != brackets_pairs[open_brackets.top()]) {
return false;
}
open_brackets.pop();
}
return open_brackets.empty();
}
};