Problem:

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.

判断输入的括号是不是合法。这题在大二的时候就遇到过了。想不到我的记性这么好。因为当时我什么都不懂,刚学数据结构,然后有个ACMer是助教,他给我讲解了这题。然后我就一直都记得了。也不知道那个师兄现在在哪里牛逼。

回到题目,就是用栈实现。

class Solution {
private:
char anti(char c)
{
if (c == ']')
return '[';
if (c == ')')
return '(';
if (c == '}')
return '{';
}
public:
bool isValid(string s) {
int len = s.size();
if (len%)
{ return false;}
if(s.size() == || s[] == ')' || s[] == ']' || s[] == '}')
return false;
stack<char> st;
for (int i = ; i < len; i++)
{
if (s[i] == '(' || s[i] == '{' || s[i] == '[')
st.push(s[i]);
else
if (st.top() == anti(s[i]))
st.pop();
else
return false;
}
if (st.empty())
return true;
else
return false;
}
};

代码写得有点挫,大概就是这个思路,然后Accept了

leetcode第20题--Valid Parentheses的更多相关文章

  1. 【LeetCode算法-20】Valid Parentheses

    LeetCode第20题 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determin ...

  2. LeetCode(20)Valid Parentheses

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

  3. LeetCode解题报告—— Longest Valid Parentheses

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

  4. [Leetcode][Python]32: Longest Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...

  5. 【一天一道LeetCode】#32. Longest Valid Parentheses

    一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...

  6. LeetCode之“动态规划”:Valid Parentheses && Longest Valid Parentheses

    1. Valid Parentheses 题目链接 题目要求: Given a string containing just the characters '(', ')', '{', '}', '[ ...

  7. 【LeetCode】32. Longest Valid Parentheses (2 solutions)

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

  8. Leetcode 题目整理-5 Valid Parentheses & Merge Two Sorted Lists

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

  9. leetcode problem 32 -- Longest Valid Parentheses

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

随机推荐

  1. Python标准库09 当前进程信息 (部分os包)

    原文:Python标准库09 当前进程信息 (部分os包) 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们在Linux的概念 ...

  2. Androids含文档erver结束(工具包 Httputils)两

    在同server在...的基础上,本文client还登录界面 Andriod简单http get请求基础上,用户注冊后跳转到下载界面,本文下载界面仅仅有两个View,一个是textView显示注冊后u ...

  3. OpenGL缓冲区

    OpenGL缓冲区 颜色缓冲区 OpenGL时,先是在一个缓冲区中完毕渲染,然后再把渲染结果交换到屏幕上. 我们把这两个缓冲区称为前颜色缓冲区(屏幕)和后颜色缓冲区.在默认情况下,OpenGL命令是在 ...

  4. POJ1080 Human Gene Functions 动态规划 LCS的变形

    题意读了半年,唉,给你两串字符,然后长度不同,你能够用'-'把它们补成同样长度,补在哪里取决于得分,它会给你一个得分表,问你最大得分 跟LCS非常像的DP数组 dp[i][j]表示第一个字符串取第i个 ...

  5. linux中fork()函数具体解释(原创!!实例解说)

     一.fork入门知识 一个进程,包含代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程差点儿全然同样的进程,也就是两个进程能够做全然同样的事,但假设初始參数或者传入的变量不 ...

  6. 酒旗少年狂暖风,至0基本的前端开发project教师们学习计划

    酒旗风暖少年狂,为0基础前端开发project师做学习计划 夜幕降暂时.走到一张废弃已久的书桌前,打开台灯,看到书桌上已经布满灰尘,而桌上的那盆羸弱的文竹已经枝繁叶茂.我擦干净了桌面,坐了下来,把买回 ...

  7. react.js 从零开始(一)

    React 是什么? 网络上的解释很多...我这里把他定义为 通过javascript 的形式组件化 html的框架... React 仅仅是 VIEW 层. React 提供了模板语法以及一些函数钩 ...

  8. CentOS 7安装配置Apache HTTP Server

    原文 CentOS 7安装配置Apache HTTP Server   RPM安装httpd # yum -yinstall httpd //安装httpd会自动安装一下依赖包: apr apr-ut ...

  9. WebApi的一种集成测试写法(in-memory)

    WebApi的一种集成测试写法(in-memory)   大家是如何对webApi写测试的呢? 1.利用Fiddler直接做请求,观察response的内容. 2.利用Httpclient做请求,断言 ...

  10. Eddy's mistakes(字母大小写转换)strlwr函数的应用

    Problem Description Eddy usually writes  articles ,but he likes mixing the English letter uses, for ...