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的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. [LeetCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  3. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  4. 更新日志(建议升级到2016.12.17) && 更新程序的方法

    更新程序的方法: 1,在控制面板里点击备份当前数据库文件到磁盘,把当天获取的信息从内存写到磁盘/存储卡.2,下载最新版的源码 wget -O "infopi.zip" " ...

  5. 72. Generate Parentheses && Valid Parentheses

    Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  6. leetcode 32. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  7. 【leetcode】Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  8. 【leetcode】 Longest Valid Parentheses (hard)★

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. [LintCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

随机推荐

  1. Mac 安装nodejs

    原文链接:http://blog.csdn.net/u010053344/article/details/50545304 Mac 安装nodejs 这几日因为需求需要又临时用到nodejs,之前安装 ...

  2. delphi ERP框架

    之前做c/s架构,接了有家装饰的一个ERP项目,做了一个ERP框架,现在转后端开发了,这些东西还是蛮怀念的,就开源出来吧,有需要的同学可以参考. https://github.com/qianlnk/ ...

  3. BZOJ3156 防御准备(动态规划+斜率优化)

    设f[i]为在i放置守卫塔时1~i的最小花费.那么显然f[i]=min(f[j]+(i-j)*(i-j-1)/2)+a[i]. 显然这是个斜率优化入门题.将不与i.j同时相关的提出,得f[i]=min ...

  4. C++ STL 常用拷贝和替换算法

    C++ STL 常用拷贝和替换算法 copy() 复制 vector<int> vecIntA; vecIntA.push_back(1); vecIntA.push_back(3); v ...

  5. 【BZOJ4709】【Jsoi2011】柠檬

    Description 传送门 题意简述:将序列划分成任意多段,从每一段选出一个数\(x\),获得\(在这一段出现的次数x*(x在这一段出现的次数)\)的贡献.求总贡献最大值. Solution ​ ...

  6. kindeditor<=4.1.5上传漏洞复现

    0x00 漏洞描述 漏洞存在于kindeditor编辑器里,你能上传.txt和.html文件,支持php/asp/jsp/asp.net,漏洞存在于小于等于kindeditor4.1.5编辑器中 这里 ...

  7. Android Studio下“Error:Could not find com.android.tools.build:gradle:2.2.1”的解决方法

    ref from: Android Studio下“Error:Could not find com.android.tools.build:gradle:2.2.1”的解决方法http://blog ...

  8. mac 10.13 build 一个 redis desktop manager

    build 的东西比较多,性能差的电脑编译会很久. 下载地址:https://redisdesktop.com/download 本来想下载一个,但是发现只有 windows 是免费的,不过官网提供了 ...

  9. Window10+Python3.5安装opencv

    Window10+Python3.5安装opencv 标签: opencvpython 2017-05-14 16:47 2201人阅读 评论(0) 收藏 举报  分类: Python编程(41)  ...

  10. 哈密顿图 哈密顿回路 哈密顿通路(Hamilton)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5452580.html 概念: 哈密顿图:图G的一个回路,若它通过图的每一个节点一次,且仅一次,就是哈密顿回路.存在哈密顿回 ...