diff options
Diffstat (limited to 'valid_parentheses.cpp')
-rw-r--r-- | valid_parentheses.cpp | 43 |
1 files changed, 0 insertions, 43 deletions
diff --git a/valid_parentheses.cpp b/valid_parentheses.cpp deleted file mode 100644 index 9bab082..0000000 --- a/valid_parentheses.cpp +++ /dev/null @@ -1,43 +0,0 @@ -// 20. Valid Parentheses -#include "leetcode.h" - -/* - * given a string 's' containing just the characters '(, ), {, }, [, ]', - * determine if the input string is valid. a input string is valid if open - * brackets must be closed by the same type of brackets. open brackets must be - * closed in the correct order. every close bracket has a corresponding open - * bracket of the same type. - */ - -class Solution { -public: - bool isValid(string s) { - stack<char> stk; - for (int i = 0; i < s.size(); i++) { - char ch = s[i]; - if (ch == '(' || ch == '{' || ch == '[') - stk.push(ch); - else { - if (!stk.empty()) { - char top = stk.top(); - if ((ch == ')' && top == '(') || (ch == '}' && top == '{') || - (ch == ']' && top == '[')) - stk.pop(); - else - return false; - } else - return false; - } - } - if (stk.empty()) - return true; - return false; - } -}; - -int main() { - Solution obj; - printf("%d\n", obj.isValid("()")); // expect: 1 - printf("%d\n", obj.isValid("()[]{}")); // expect: 1 - printf("%d\n", obj.isValid("(]")); // expect: 0 -} |