题目

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.

分析

典型的括号匹配问题,数据结构一书中的典型实例。其程序实现,借助栈结构是最佳方法。

AC代码

class Solution {
public:
bool isValid(string s) {
int len = strlen(s.c_str());
if (len == 0)
return true;
if (len % 2 != 0)
return false; //括号匹配问题,用数据结构栈辅助实现
stack<char> sta;
for (int i = 0; i < len; i++)
{
if (s[i] != ')' && s[i] != ']' && s[i] != '}')
sta.push(s[i]);
else
{
if (sta.size() <= 0)
return false;
if (s[i] == PairLetter(sta.top()))
sta.pop();
else
return false;
}//else
}//for
if (sta.size() == 0)
return true;
else
return false;
} char PairLetter(const char &c)
{
switch (c)
{
case '(':
return ')'; break;
case '[':
return ']'; break;
case '{':
return '}'; break;
default:
return 0; break;
}
}
};

GitHub测试程序源码

LeetCode(20)Valid Parentheses的更多相关文章

  1. LeetCode(49)-Valid Parentheses

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

  2. 【LeetCode算法-20】Valid Parentheses

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

  3. LeetCode(36)Valid Sudoku

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  4. LeetCode(242)Valid Anagram

    题目 Given two strings s and t, write a function to determine if t is an anagram of s. For example, s ...

  5. leetcode第20题--Valid Parentheses

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

  6. LeetCode(20):有效的括号

    Easy! 题目描述: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭 ...

  7. LeetCode(125) Valid Palindrome

    题目 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ign ...

  8. LeetCode(22)Generate Parentheses

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  9. LeetCode(65) Valid Number

    题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...

随机推荐

  1. php 获得上周数据

    $lastMondy = date('Y-m-d', strtotime('-2 sunday +1 days', time()));$lastSundy = date('Y-m-d', strtot ...

  2. AppStore 审核拒绝原因记录

    此文仅记录审核app被拒绝的原因 1.未提供充值功能,app中出现vip或者会员图标文字 被拒 解决,隐藏或取消该图标或文字 2.第三方登录,需要跳转到第三方app登录 被拒 解决,审核时隐藏第三方登 ...

  3. 跟我一起玩Win32开发(18):使用对话框的两个技巧

    相信大家知道对话框怎么用了,就是先用“资源编辑器”设计一个对话框,然后在代码中加载处理.今天,我向大家分享两个使用对话框的技巧,还是比较实用的.不用担心,先喝杯茶,很简单的,一点也不复杂,总之,看俺写 ...

  4. bzoj 4821 [Sdoi2017]相关分析

    题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4821 题解 做法显然 就是维护一颗线段树 里面装4个东西 区间x的和 区间y的和 区间$x^ ...

  5. 洛谷 P3810 【模板】三维偏序(陌上花开) (cdq分治模板)

    在solve(L,R)中,需要先分治solve两个子区间,再计算左边区间修改对右边区间询问的贡献. 注意,计算额外的贡献时,两子区间各自内部的顺序变得不再重要(不管怎么样左边区间的都发生在右边之前), ...

  6. Backbone学习记录(5)

    数据与服务器 var User=Backbone.Model.extend({ defaults:{ name:'susan', age:18 }, url:'/user'//数据提交的路径 }); ...

  7. python_10(模块与包)

    第1章 模块 1.1 模块的种类 1.2 定义 1.3 作用 1.4 导入及使用 1.4.1 import 1.4.2 测试一: 1.4.3 测试二: 1.4.4 测试三: 1.4.5 小结 1.4. ...

  8. 机器学习概念之特征选择(Feature selection)

    不多说,直接上干货! .

  9. XSS漏洞解析(二)

    上篇我们讲了XSS的一些相关的内容,这篇我们就直接上代码demo解决实际问题吧. 主要的问题是xssfilter的编写,我们直接去网上找一下框架,一般有js,php,java等语言都有相关的XSS的相 ...

  10. nginx 80端口重定向到443端口

    server { listen ; server_name www.域名.com; rewrite ^(.*)$ https://${server_name}$1 permanent; } serve ...