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. mybaits入门(含实例教程和源码) http://blog.csdn.net/u013142781/article/details/50388204

    前言:mybatis是一个非常优秀的存储过程和高级映射的优秀持久层框架.大大简化了,数据库操作中的常用操作.下面将介绍mybatis的一些概念和在eclipse上的实际项目搭建使用. 一.mybati ...

  2. 我项目中使用userData的实例 UserData.js

    关于userData的介绍,请参见http://hi.baidu.com/kaisep/blog/item/1da9a3312d2da5a15edf0e87.htmlhttp://hi.baidu.c ...

  3. 概率图模型(PGM)综述-by MIT 林达华博士

    声明:本文转载自http://www.sigvc.org/bbs/thread-728-1-1.html,个人感觉是很好的PGM理论综述,高屋建瓴的总结了PGM的主要分支和发展趋势,特收藏于此. “概 ...

  4. libuv 简单使用

    libuv 简单使用 来源:https://zhuanlan.zhihu.com/p/50497450 前序:说说为啥要研究libuv,其实在很久之前(大概2年前吧)玩nodejs的时候就对这个核心库 ...

  5. 沉迷AC自动机无法自拔之:穿越广场 square

    如标题所言,我已经沉迷于AC自动机无法自拔了... 这又是一道AC自动的题,红红火火恍恍惚惚 穿越广场 [问题描述] L 国的仪仗队要穿越首都广场了.首都广场可以看做是一块 N*M 的矩形网格,仪仗队 ...

  6. BZOJ 3357: [Usaco2004]等差数列

    3357: [Usaco2004]等差数列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 338  Solved: 160[Submit][Statu ...

  7. echarts分组柱状图的前后台处理 带平均线显示

    原生的echarts使用: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /&g ...

  8. mac 10.13 build 一个 redis desktop manager

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

  9. 使用 python 将 "\r\n" 转换为 "\n"

    众所周知, Linux 下没有 "\r\n", 而 windows 下文本工具默认打开文件时使用 t 模式, 使得写入一行结尾的换行符为 "\r\n", 这样造 ...

  10. 【题解】【THUSC 2016】成绩单 LOJ 2292 区间dp

    Prelude 快THUWC了,所以补一下以前的题. 真的是一道神题啊,网上的题解没几篇,而且还都看不懂,我做了一天才做出来. 传送到LOJ:(>人<:) Solution 直接切入正题. ...