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.

原题链接:https://oj.leetcode.com/problems/valid-parentheses/

题目:给定一个仅包括'('')''{''}''[' 和 ']' 的字符串,检測输入的串是否合法。括号是否配对。

思路:使用一个栈。遇到左括号则压入。遇到右括号则与左括号检測是否匹配,不匹配即false,弹出顶元素,循环。

	public boolean isValid(String s){
int len = s.length();
if(len <= 1)
return false;
if(s.charAt(0) == ')' || s.charAt(0)==']' || s.charAt(0)=='}')
return false;
Stack<Character> stack = new Stack<Character>();
for(int i=0;i<len;i++){
if(s.charAt(i) == '(' || s.charAt(i)=='[' || s.charAt(i)=='{')
stack.push(s.charAt(i));
else{
if(stack.size() == 0)
return false;
if(s.charAt(i) == ')')
if(stack.peek() != '(')
return false;
if(s.charAt(i) == ']')
if(stack.peek() != '[')
return false;
if(s.charAt(i) == '}')
if(stack.peek() != '{')
return false;
stack.pop();
}
}
return stack.size() == 0;
}



LeetCode——Valid Parentheses的更多相关文章

  1. [LeetCode] Valid Parentheses 验证括号

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

  2. LeetCode: Valid Parentheses 解题报告

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

  3. [Leetcode] valid parentheses 有效括号对

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

  4. [LeetCode] Valid Parentheses

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

  5. leetcode—Valid Parentheses

    1.问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if t ...

  6. Python3解leetcode Valid Parentheses

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

  7. leetcode Valid Parentheses python

    # 解题思路: # 创建一个字典映射关系 dicts# 使用一个栈stk 遍历字符串s 得到一个新的字符串curItem 如果lastItem在dicts中的value和它相等 不做任何操作# 如果不 ...

  8. LeetCode Valid Parentheses 有效括号

    class Solution { public: void push(char c){ //插入结点 struct node *n=new struct node; n->nex=; n-> ...

  9. Valid Parentheses [LeetCode 20]

    1- 问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...

随机推荐

  1. FZU2150 Fire Game

    题目: 两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地方时间为0,蔓延至下一格的时间依次加一.求烧完所有的草需要的最少时间 ...

  2. .net core虚拟目录配置

    .net core在iis上配置虚拟目录不起作用,只需要将app.UseStaticFiles中物理路径PhysicalFileProvider改为指定的路径即可,如:E:\\Html\\News a ...

  3. The name ‘InitialzeComponent’ does not exist in the current context

    在Visual Studio中创建Windows Store项目,在MainPage.xaml.cs中出现错误: The name 'InitialzeComponent' does not exis ...

  4. 使用C#正则表达式获取必应每日图片地址

    微软的Bing搜索引擎首页每天都会提供了一些有趣的图片,下面使用正则表达式获取图片的地址,不管是在手机app还是在网站上都是很好的图片素材,而且每天更新,非常不错. 首先访问微软的API,该地址返回的 ...

  5. 全局变量变为局部变量 & MVC思想

    1 函数中的全局变量如何变成局部变量? 全局变量之间会相互骚扰.所以在代码中不要用全局变量.ES6之前只有函数里面有全局变量. 全局变成局部变量怎么变? 把代-放在一个函数如中,再.call()执行一 ...

  6. Android学习——利用RecyclerView编写聊天界面

    1.待会儿会用到RecyclerView,首先在app/build.gradle(注意有两个build.gradle,选择app下的那个)当中添加依赖库,如下: dependencies { comp ...

  7. 在maven项目结构下对于Resources目录下文件的存取

    在maven项目中,文件结构如下: proj ---src ----main ----java ----Main.java ----resources ----userFile.properties ...

  8. git基础讲解

    idea :https://blog.csdn.net/autfish/article/details/52513465 eclipse:https://blog.csdn.net/yang57266 ...

  9. WPF创建自定义控件并运用

    此项目源码:https://github.com/lizhiqiang0204/WpfCustomControlLibrary1 首先创建自定义控件库项目 项目名称命名为:WpfCustomContr ...

  10. JS 蓝球弹起的高度 100 米 第几次高度小于1米

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...