2016.6.17——Valid Parentheses
Valid Parentheses
本题收获:
1.stack的使用
2.string和char的区别
题目:
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
注意题目中只是输入了一个字符串 如:“{}(]” 而不是{“{}[”,"[]"}
思路:
leetcode:用stack,括号为左边压入栈,右边的和栈顶对比,所有的都匹配返回true,不匹配返回false
代码:
bool isValid(string s) {
stack<char> st;
for(char c : s){
if(c == '('|| c == '{' || c == '['){
st.push(c);
}else{
if(st.empty()) return false;
if(c == ')' && st.top() != '(') return false;
if(c == '}' && st.top() != '{') return false;
if(c == ']' && st.top() != '[') return false;
st.pop();
}
}
return st.empty();
我的测试代码:
class MyClass
{
public:
bool isValid(string str)
{
stack<char> st; //is <char> not<string>
for (size_t i = ; i < str.size(); i++)
{
if (str[i] == '(' || str[i] == '{' || str[i] == '[')
{
st.push(str[i]);
}
else
{
if (str[i] == ')' && st.top() != '(') return false;
if (str[i] == ']' && st.top() != '[') return false;
if (str[i] == '}' && st.top() != '{') return false; //不写st.pop()有什么差别
}
}
return true; //st.empty()
}
};
完整代码:
// Valid Parentheses.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include "iostream"
#include "stack"
#include "stack"
using namespace std; class MyClass
{
public:
bool isValid(string str)
{
stack<char> st; //is <char> not<string> 栈的定义,注意是string/char
for (size_t i = ; i < str.size(); i++)
{
if (str[i] == '(' || str[i] == '{' || str[i] == '[')
{
st.push(str[i]);
}
else
{
if (str[i] == ')' && st.top() != '(') return false;
if (str[i] == ']' && st.top() != '[') return false; //st.top(),有括号“,”栈的.后面都有()
if (str[i] == '}' && st.top() != '{') return false; //不写st.pop()有什么差别
}
}
return true; //st.empty()
}
};
/*
class MyClass
{
public:
bool isValid(string str)
{
stack<char> st; //is <char> not<string>
for (char c : str)
{
if (c == '(' || c == '{' || c == '[')
{
st.push(c);
}
else
{
if (c == ')' && st.top() != '(') return false;
if (c == ']' && st.top() != '[') return false;
if (c == '}' && st.top() != '{') return false;
st.pop();
}
}
return st.empty();
} };*/ int _tmain(int argc, _TCHAR* argv[])
{
string str = "({[]})";
int m = ;
MyClass solution;
m = solution.isValid(str);
cout << m << endl;
system("pause");
return ;
}
2016.6.17——Valid Parentheses的更多相关文章
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 更新日志(建议升级到2016.12.17) && 更新程序的方法
更新程序的方法: 1,在控制面板里点击备份当前数据库文件到磁盘,把当天获取的信息从内存写到磁盘/存储卡.2,下载最新版的源码 wget -O "infopi.zip" " ...
- 72. Generate Parentheses && Valid Parentheses
Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【leetcode】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【leetcode】 Longest Valid Parentheses (hard)★
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LintCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- mybatis的setting
在mybaits中,setting的的配置参数如下(如果不在配置文件中配置将使用默认值): 设置参数 描述 有效值 默认值 cacheEnabled 该配置影响的所有映射器中配置的缓存的全局开关 tr ...
- ansible操作(一)
ansible晋级操作之ad-hoc命令 所谓的ad-hoc命令! 如果我们敲入一些命令去比较快的完成一些事情,而不需要将这些执行的命令特别保存下来, 这样的命令就叫做 ad-hoc 命令.Ansib ...
- 基于element-ui的后台系统表格、dialog、筛选、自定义按钮、分页的一次性封装
方便基础业务开发封装的一套组件,基于vue2.5.x和element-ui,可以通过配置自动生成表格展示,表格新增.编辑功能.分页.筛选项.自定义显示表格数据等功能. 先上演示图片 --------- ...
- Python - 静态函数(staticmethod), 类函数(classmethod), 成员函数 区别(完全解析)
原文地址:http://blog.csdn.net/caroline_wendy/article/details/23383995 还有一篇:http://blog.csdn.net/carolzha ...
- mysql中LIKE和REGEXP
mysql中LIKE和REGEXP都可以用来字符匹配 正则表达式REGEXP是为复杂搜索指定模式的强大方式. like用法 LIKE一般与通配符(%)和(_)两个使用 如例 SELECT prod ...
- PHP-从零开始使用Solr搜索引擎服务(下)
前言: 原文地址: http://www.cnblogs.com/JimmyBright/p/7156085.html 前面在配置完成Solr服务之后,在浏览器上可以打开Solr的管理界面,这个界面几 ...
- 【BZOJ1034】泡泡堂(贪心)
[BZOJ1034]泡泡堂(贪心) 题面 BZOJ 洛谷 题解 很基础的贪心,然而我竟然没写对...身败名裂. 大概就是类似田忌赛马. 先拿看当前最大值是否能否解决对面最大值,否则检查能否用最小值来兑 ...
- 【ARC069F】Flags
Description 数轴上有 \(n\)个旗子,第\(i\)个可以插在坐标\(x_i\)或者\(y_i\). 请最大化两两旗子之间的最小距离. \(2 \le n \le 10^4\),\ ...
- 《Linux内核设计与实现》学习总结 Chap5
一.与内核通信 1.系统调用在用户空间进程和硬件设备之间添加了一个中间层. 作用: 1)为用户空间提供了一种硬件的抽象接口. 2)系统调用保证了系统的稳定和安全. 3)每个进程都运行在虚拟系统中,而在 ...
- adb server version (32) doesn't match this client (36); killing...
http://blog.csdn.net/seaker_/article/details/55107598 FAQ: adb server version (36) doesn't match thi ...