LintCode: Valid Parentheses
C++
stack<char|int|string>, push(), pop(), top(), empty(), size()
1 class Solution { 2 public: 3 /** 4 * @param s A string 5 * @return whether the string is a valid parentheses 6 */ 7 bool isValidParentheses(string& s) { 8 // Write your code here 9 stack<char> sta; 10 for (int i = 0; i < s.size(); i++) { 11 if (s[i] == '(' || s[i] == '{' || s[i] == '[') { 12 sta.push(s[i]); 13 continue; 14 } 15 char top = sta.top(); 16 sta.pop(); 17 if (s[i] == ')' && top != '(') return false; 18 if (s[i] == ']' && top != '[') return false; 19 if (s[i] == '}' && top != '{') return false; 20 } 21 return sta.empty(); 22 } 23 };
![字节跳动内推](https://img2020.cnblogs.com/blog/323808/202004/323808-20200415172728571-1992447742.jpg)
找我内推: 字节跳动各种岗位
作者:
ZH奶酪(张贺)
邮箱:
cheesezh@qq.com
出处:
http://www.cnblogs.com/CheeseZH/
*
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。