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 ...
随机推荐
- 【算法】—— 1到n中减少了一个数,顺序被打乱,找出缺失的数
问题 有0-n这n+1个数,但是其中丢了一个数,请问如何找出丢了哪个数? 五种方法 1)用1+2+...+n减去当前输入数据的总和.时间复杂度:O(n) 空间复杂度:O(1) [容易溢出] 2)用12 ...
- window.location.hash 使用
[转]http://www.cnblogs.com/nifengs/p/5104763.html location是javascript里边管理地址栏的内置对象,比如location.href就管理页 ...
- 解决MySQL复制出错 Last_SQL_Errno:1146
背景:我们在做数据迁移或者拆分的时候,使用Tablespace transcation 这种解决方案时,很有可能就会遇到 从库复制出错,报: Last_SQL_Errno: 1146 那么具体错误内容 ...
- Qt——基本工具的使用
本文主要介绍在windows系统中使用C++编写Qt程序所需要的一些工具,不会具体地讲工具怎么使用. 其它系统的安装本文不会涉及,在http://wiki.qt.io/Main中,有关于各种系统qt安 ...
- 记一次java程序out of memory问题
在一个比较大批量的pdf转String项目中遇到了:java.lang.OutOfMemoryError: Java heap space错误 第一反应肯定是程序没有写好,大量循环时没有把程序中没有用 ...
- 【比赛】NOIP2017 总结
一.比赛过程 Day1: 拿到题目后,立即把所有题目都看了一遍,发现没有很虚的期望DP和概率DP,感到很庆幸.然后发现今年的题目顺序好像有点不对,T1是数论,T2像是模拟,这难道是把两天的基础题放到一 ...
- 学习Spring Boot:(九)统一异常处理
前言 开发的时候,每个controller的接口都需要进行捕捉异常的处理,以前有的是用切面做的,但是SpringMVC中就自带了@ControllerAdvice ,用来定义统一异常处理类,在 Spr ...
- 【JQuery】DOM元素
一.前言 接着上一章的内容,继续本章的学习. 二.内容 .get 获得由选择器指定的DOM元素, 可输入匹配元素的index编号 $(selector).get(index) .ind ...
- LINUX第四周学习
<Linux内核设计与实现>第四周读书笔记——第五章 5.1 与内核通信57 系统调用在用户空间进程和硬件设备之间添加了一个中间层,该层主要作用有三个: 首先它为用户空间提供了一种硬件的抽 ...
- Linux内核设计第六周学习总结 分析Linux内核创建一个新进程的过程
陈巧然 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.实验过程 登陆实验楼 ...