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. CCBValue

    #ifndef __CCB_VALUE_H__ #define __CCB_VALUE_H__ #include "cocos2d.h" #include "Extens ...

  2. c# 硬件开源神器netduino的开发中慎用Cpu.Pin

    最近为了测试netduino开发板的各个端口是否正常使用,让同事写了一些测试程序,结果出了问题,他的测试程序导致开发板无法发布程序进去,按他的结论是开发板有问题,针对这个情况,我们经过仔细分析代码,认 ...

  3. Codeforces 439C Devu and Partitioning of the Array(模拟)

    题目链接:Codeforces 439C Devu and Partitioning of the Array 题目大意:给出n个数,要分成k份,每份有若干个数,可是仅仅须要关注该份的和为奇数还是偶数 ...

  4. HTML5分析实战WebSockets一个简短的引论

    HTML5 WebSockets规范定义了API,同意web页面使用WebSockets与远程主机协议的双向通信. 介绍WebSocket接口,并限定了全双工通信信道,通过套接字网络. HTML5 W ...

  5. iphone手机版降级

    想减少移动版本号.你能下载下来你想要的版本在苹果开发者网站,或"信誉"或可以下载到其他网站的iPhone下载固件固件网站.然后打开iTunes,进入 然后按着(alt).同一时候用 ...

  6. 【C语言的日常实践(八)】弦

    串数据类型是一个重要的,但C有没有明确的语言字符串数据类型.头文件string.h它包括大多数字符串处理函数. 故,有操作的串.通常包括string.h头文件. 1.字符串的长度:对字符串进行的操作中 ...

  7. Matrix (二维树状数组)

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

  8. Git批量删除

    Git批量删除 git的改动都需要stage过程后才能commit.当git中有大量改动时就需要能够批量操作在方便.改动分三种: modify: 有文件修改 add: 有文件增加 rm: 有文件删除 ...

  9. 6最好的之一 HTML5/CSS3 演示(PPT)框架

    HTML 是全世界最流行的网页编程语言.而HTML5是这门语言的升级版本号.越来越多的开发人员和设计师開始使用HTML5.以下介绍几个 HTML5/CSS3 的演示工具和框架,你能够用它们来创建你的网 ...

  10. EF 事物

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...