LeetCode20题不多说上代码

public boolean isValid(String s){
Stack<Character> stack = new Stack<Character>();
for (int i=0;i<s.length();i++){
char c = s.charAt(i);
if (c=='('||c=='['||c=='{')
stack.push(c);
else{
//栈没字符匹配失败
if (stack.isEmpty())
return false;
char topChar = stack.pop();
if (c==')'&&topChar!='(')
return false;
if (c==']'&&topChar!='[')
return false;
if (c=='}'&&topChar!='{')
return false; }}
return stack.isEmpty();
}

LeetCode第20题的更多相关文章

  1. leetcode第20题--Valid Parentheses

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

  2. LeetCode 第20题--括号匹配

    1. 题目 2.题目分析与思路 3.代码 1. 题目 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭 ...

  3. LeetCode第[20]题(Java):Valid Parentheses

    题目:有效的括号序列 难度:Easy 题目内容: Given a string containing just the characters '(', ')', '{', '}', '[' and ' ...

  4. LeetCode第20题:有效的括号

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

  5. 【LeetCode算法-20】Valid Parentheses

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

  6. 【JavaScript】【dp】Leetcode每日一题-解码方法

    [JavaScript]Leetcode每日一题-解码方法 [题目描述] 一条包含字母 A-Z 的消息通过以下映射进行了 编码 : 'A' -> 1 'B' -> 2 ... 'Z' -& ...

  7. 【python】Leetcode每日一题-二叉搜索迭代器

    [python]Leetcode每日一题-二叉搜索迭代器 [题目描述] 实现一个二叉搜索树迭代器类BSTIterator ,表示一个按中序遍历二叉搜索树(BST)的迭代器: BSTIterator(T ...

  8. 【python】Leetcode每日一题-螺旋矩阵2

    [python]Leetcode每日一题-螺旋矩阵2 [题目描述] 给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix . ...

  9. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

随机推荐

  1. python的type class

    在python中,用户定义的class是一个PyTypeObject ( XXX_Type)对象. #PyType_Type是一切类的基类,这是一个全局数据PyTypeObject PyType_Ty ...

  2. UDP通讯协议实例

    1.服务端 import java.io.IOException; import java.net.*; public class UDPDemo { public static void main( ...

  3. 尚硅谷springboot学习5-主入口类说明

    package com.atguigu; import org.springframework.boot.SpringApplication; import org.springframework.b ...

  4. RabbitMQ系列教程之四:路由(Routing)(转载)

    RabbitMQ系列教程之四:路由(Routing) (使用Net客户端) 在上一个教程中,我们构建了一个简单的日志系统,我们能够向许多消息接受者广播发送日志消息. 在本教程中,我们将为其添加一项功能 ...

  5. 福州大学软件工程W班-助教总结

    背景 福州大学软件工程W班,总人数46人,讲师汪老师. 前期期望 希望自己能够在课程当中起到引导作用,发挥助教最大的用处. 实际执行情况 第一个问题是自动化测试工具,该工具主要是用来测试程序WordC ...

  6. IPv4和IPv6的差异;如何实现IPv4和IPv6双协议栈的通信

    1 IPv4和IPv6的差异 1.1 地址空间   IPv6 与 IPv4 比较最显著的一个改动就是使用 128 比特上的地址来代替了 32 比特长的 IPv4 地址. IPv6 中取消了广播地址, ...

  7. Python读写文件基础.py

    基本函数 定义 python内置了open()函数来操作文件,open()函数的定义为: open(file, mode='r', buffering=-1, encoding=None, error ...

  8. windows 10 专业版 激活

    参考文章:https://jingyan.baidu.com/article/c14654134b99de0bfcfc4c8c.html http://www.windowszj.com/news/2 ...

  9. editable : false与 readonly 的区别

    editable : false 不能输入 readonly:不可操作,只能看

  10. 吴裕雄 python神经网络 花朵图片识别(9)

    import osimport numpy as npimport matplotlib.pyplot as pltfrom PIL import Image, ImageChopsfrom skim ...