-
Notifications
You must be signed in to change notification settings - Fork 0
20. Valid Parentheses #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)){ | ||
stack.pop(); | ||
}else{ | ||
stack.push(c); | ||
} | ||
} | ||
return stack.empty(); | ||
} | ||
|
||
bool isPair(char l, char r){ | ||
return (l == '(' && r == ')' || l == '{' && r == '}' || l == '[' && r == ']'); | ||
} | ||
}; |
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 == ']'); | ||
} | ||
}; |
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){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 == ']'); | ||
} | ||
}; |
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] == ']'){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for文の中で考慮できると思うんですよね。 要はclose bracketsが登場した際に、stackが空であればこのタイミングでFalseとうことが分かります。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. あー、なるほど、ありがとうございます、その発想はなかったです。 |
||
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){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. この関数は必要なのでしょうか? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return (l == '(' && (r == '}' || r == ']')) || (l == '{' && (r == ')' || r == ']')) || (l == '[' && (r == '}' || r == ')')); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あくまで個人的な感覚なのですが、対応の取れないかっこが現れた時点で
return false
してよいように思いました。Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど、c
が{
,(
,[
などであれば、ペアにしようがないので、メソッド呼び出しが不要ですね勘違いしました
(}
みたいなのが出てきた時点でreturn false
可能ということですね。