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
20 changes: 20 additions & 0 deletions 20/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
bool isValid(string s) {
stack<char> stack;

for(char c : s){
// stack topとのチェック
if(!stack.empty() && isPair(stack.top(), c)){
Copy link

Choose a reason for hiding this comment

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

あくまで個人的な感覚なのですが、対応の取れないかっこが現れた時点で return false してよいように思いました。

Copy link
Owner Author

@colorbox colorbox Mar 12, 2024

Choose a reason for hiding this comment

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

なるほど、c{,(,[などであれば、ペアにしようがないので、メソッド呼び出しが不要ですね

勘違いしました(}みたいなのが出てきた時点でreturn false可能ということですね。

stack.pop();
}else{
stack.push(c);
}
}
return stack.empty();
}

bool isPair(char l, char r){
return (l == '(' && r == ')' || l == '{' && r == '}' || l == '[' && r == ']');
}
};
19 changes: 19 additions & 0 deletions 20/2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
bool isValid(string s) {
stack<char> stack;

for(char c : s){
if(!stack.empty() && isPair(stack.top(), c)){
stack.pop();
}else{
stack.push(c);
}
}
return stack.empty();
}

bool isPair(char l, char r){
return (l == '(' && r == ')' || l == '{' && r == '}' || l == '[' && r == ']');
}
};
20 changes: 20 additions & 0 deletions 20/3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
bool isValid(string s) {
stack<char> stack;

for(char c: s){

Choose a reason for hiding this comment

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

}()()()()などの文字列は最後まで見ないでも1文字目を見ただけで有効でないと分かると思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かに、最初の一文字を調べるだけで早期returnが可能ですね

if(!stack.empty() && isPair(stack.top(), c)){
stack.pop();
}else{
stack.push(c);
}
}

return stack.empty();
}

bool isPair(char l, char r){
return (l == '{' && r == '}' || l == '(' && r == ')' || l == '[' && r == ']');
}
};
29 changes: 29 additions & 0 deletions 20/4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
bool isValid(string s) {
stack<char> stack;

if(s[0] == ')' || s[0] == '}' || s[0] == ']'){

Choose a reason for hiding this comment

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

for文の中で考慮できると思うんですよね。
以前の指摘と似ていますが、[]([][][][]の場合は3文字目を見たタイミングで早期リターンできます。

要はclose bracketsが登場した際に、stackが空であればこのタイミングでFalseとうことが分かります。

Copy link
Owner Author

@colorbox colorbox Mar 13, 2024

Choose a reason for hiding this comment

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

あー、なるほど、ありがとうございます、その発想はなかったです。
確かにclose bracketとstackの状況を参照するだけでfalseにできますね・・・

return false;
}

for(char c : s){
if(!stack.empty() && isInvalidPair(stack.top(), c)){return false;}

if(c != '(' && c != '{' && c != '[' && !stack.empty() && isPair(stack.top(), c)){
stack.pop();
}else{
stack.push(c);
}
}
return stack.empty();
}

bool isPair(char l, char r){
return (l == '(' && r == ')' || l == '{' && r == '}' || l == '[' && r == ']');
}

bool isInvalidPair(char l, char r){

Choose a reason for hiding this comment

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

この関数は必要なのでしょうか? isPairを反転させれば良い気がします。

Copy link
Owner Author

Choose a reason for hiding this comment

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

isPairの反転と厳密には等価ではないのと
isPairの反転だと分かりづらいため、このメソッドがあっても良いかなと思ってます。

return (l == '(' && (r == '}' || r == ']')) || (l == '{' && (r == ')' || r == ']')) || (l == '[' && (r == '}' || r == ')'));
}
};