public class Solution {
Stack<char> S = new Stack<char>(); public bool IsValid(string s)
{
foreach (var c in s)
{
if (c == '(' || c == '[' || c == '{')
{
S.Push(c);
}
else if(c == ')')
{
if (S.Count > )
{
var k = S.Peek();
if (k == '(')
{
S.Pop();
}
else
{
return false;
}
}
else
{
return false;
}
}
else if (c == ']')
{
if (S.Count > )
{
var k = S.Peek();
if (k == '[')
{
S.Pop();
}
else
{
return false;
}
}
else
{
return false;
}
}
else if (c == '}')
{
if (S.Count > )
{
var k = S.Peek();
if (k == '{')
{
S.Pop();
}
else
{
return false;
}
}
else
{
return false;
}
}
}
if (S.Count > )
{
return false;
}
else
{
return true;
}
}
}

https://leetcode.com/problems/valid-parentheses/#/description

补充一个python的实现:

 class Solution:
def isValid(self, s: str) -> bool:
n = len(s)
if n == :
return True
stack = []
for c in s:
if c == '(' or c == '{' or c == '[':
stack.append(c)
elif c == ')':
if len(stack) > :
if stack[-] == '(':
stack.pop(-)
else:
stack.append(')')
else:
return False
elif c == '}':
if len(stack) > :
if stack[-] == '{':
stack.pop(-)
else:
stack.append('}')
else:
return False
elif c == ']':
if len(stack) > :
if stack[-] == '[':
stack.pop(-)
else:
stack.append(']')
else:
return False
if len(stack) == :
return True
else:
return False

leetcode20的更多相关文章

  1. LeetCode20 Valid Parentheses

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

  2. [Swift]LeetCode20. 有效的括号 | Valid Parentheses

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

  3. LeetCode20:validParentheses

    validParentheses 题目描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  4. leetcode20:有效的括号

    给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认 ...

  5. [LeetCode-20]Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

  6. LeetCode20.有效的括号 JavaScript

    给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认 ...

  7. LeetCode20——有效的括号

    在记事本中写算法题和在纸上写其实感觉差不多,反正是不能进行调试.想起某高手的话,写代码要做到“人机合一”,写高级语言时(指的是 C 和 C++)脑海中要知道当前写的代码对应的反汇编代码,也就是要深入了 ...

  8. <每日 1 OJ> -LeetCode20. 有效的括号

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

  9. 面试之leetcode20堆栈-字符串括号匹配,队列实现栈

    1 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合.左括号必须以正确的顺序闭合.注意空字符串可被认 ...

随机推荐

  1. LNMP(二)

    第二十一课 LNMP(二) 目录 一.默认虚拟主机 二.Nginx用户认证 三.Nginx域名重定向 四.Nginx访问日志 五.Nginx日志切割 六.静态文件不记录日志和过期时间 七.Nginx防 ...

  2. python函数进阶(函数参数、返回值、递归函数)

    函数进阶 目标 函数参数和返回值的作用 函数的返回值 进阶 函数的参数 进阶 递归函数 01. 函数参数和返回值的作用 函数根据 有没有参数 以及 有没有返回值,可以 相互组合,一共有 4 种 组合形 ...

  3. TestLink工具使用手册介绍

    工具名称:TestLink 工具介绍:TestLink遵循Apache2开源协议,免费试用.TestLink用于进行测试过程中的管理,通过使用TestLink提供的功能,可以将测试过程从测试需求.试设 ...

  4. easyui,文件引用

  5. IDEA 类图功能使用方法

    1. Ctrl+Shift+Alt+U显示类图,(可以选中代码中类,再按快捷键,直接进入此类的类图) 2. 在类图中,选中某类右击显示Show Implementations,弹出子类的选择框. 按S ...

  6. linux 命令失效

    失效的原因 是我在执行命令的时候输入错误了.在网上找了很多的办法都是以下两种方式:  其一:直接在linux命令行界面输入如下,然后回车(导入环境变量,以及shell常见的命令的存放地址):  exp ...

  7. Floodlight1.2+Mininet的安装及使用

    直接转载的大神的,亲测Ubuntu16.04版本没问题 https://www.sdnlab.com/19189.html

  8. BSGS模板(慢速)

    //author Eterna #define Hello the_cruel_world! #pragma GCC optimize(2) #include<iostream> #inc ...

  9. 2.1 Visio画图后,粘贴到word白边太宽

    如下图所示:Visio2007画图后,图白边距很宽. 右击打开>将鼠标移动到画布边缘,按下Ctrl后鼠标变为双箭头,然后拖拽方格画布,拖拽合适的宽度保存即可.

  10. hadoop完全分步式搭建

    实验环境介绍 4台机器,规划如下: 计算机名 IP地址 角色 master 192.168.138.200 NameNode,SecondaryNameNode,ResourceManager sla ...