Lintcode C++代码
来源:互联网 发布:盛势网络剧视频第四集 编辑:程序博客网 时间:2024/06/07 09:55
给定一个字符串所表示的括号序列,包含以下字符: ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, 判定是否是有效的括号序列。
主要用用到堆栈 操作
不熟悉的小伙伴,自己恶补!
以下是一位博主的的代码;
思路:
1 如果是左半部分,进栈
2 右半部分,栈顶元素出栈,判断是否对应,对应删除该元素,否则返回false
class Solution {public: /** * @param s A string * @return whether the string is a valid parentheses */ bool isValidParentheses(string& s) { // Write your code here int n = s.length(); stack<char> buf; for (int i = 0; i <n; i++) { if (s[i] == '(' || s[i] == '{' || s[i] =='[') { buf.push(s[i]); } else if (s[i] == ')') { if (!buf.empty() && buf.top() == '(') { buf.pop(); } else { return false; } } else if (s[i] == '}') { if (!buf.empty() && buf.top() == '{') { buf.pop(); } else { return false; } } else if (s[i] == ']') { if (!buf.empty() && buf.top() == '[') { buf.pop(); } else { return false; } } } return buf.empty(); }};
阅读全文
0 0
- Lintcode 代码
- Lintcode C++代码
- Lintcode C++代码
- Lintcode C++代码
- LintCode 代码 翻转数
- [LintCode]Sort Integers(C++)
- [LintCode]strStr(C++)
- lintcode 通配符匹配 ac代码
- lintcode python代码 420 报数
- lintcode python代码 80中位数
- LintCode C++代码旋转字符串
- LintCode C++代码Two Sum
- lintcode 通配符匹配 ac代码
- [LintCode]Compare Strings(C++|Python)
- LintCode-第一个错误的代码版本
- LintCode 第一个错误的代码版本
- LintCode : 第一个错误的代码版本
- LintCode : 第一个错误的代码版本
- 强化学习及Python代码示例
- HTML-标签语义化
- c语言结构体学习时写的小代码
- 对指令集的一点理解
- 2017 CBDS第四届中国国际大数据大会9月26日北京召开
- Lintcode C++代码
- 如何用Tensorflow训练模型成pb文件和和如何加载已经训练好的模型文件
- WPF面试知识要点
- 使用SOCKET实现TCP/IP协议的通讯
- ORA-29857:表空间中存在域索引和/或次级对象
- myeclipse中如何修改Servlet模板
- 剑指offer之从尾到头打印链表
- webstorm2017.02破解(亲测有效)
- Picasso从入门到源码解析