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. kubernetes组件架构

    kubernetes,简称K8s,是用8代替8个字符“ubernete”而成的缩写.是一个开源的,用于管理云平台中多个主机上的容器化的应用,Kubernetes的目标是让部署容器化的应用简单并且高效( ...

  2. Word,excel开发指南

    New Document dfsdfds &dsfds &sdf; dsf dsf dsfds fsdfdsfdsf dsfs dsfds dsf dsfd sfds   sdf fd ...

  3. leetcode1015

    class Solution(object): def smallestRepunitDivByK(self, K: int) -> int: if K % 2 == 0 or K % 5 == ...

  4. leetcode44

    public boolean isMatch(String text, String pattern) { // 多一维的空间,因为求 dp[len - 1][j] 的时候需要知道 dp[len][j ...

  5. js 验证码倒计时

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. AIR程序调用本地默认应用程序打开本地文件

    当我用下面语句的时候,可以成功打开桌面文件夹下面的文件: var file:File = File.desktopDirectory.resolvePath("test.jpg") ...

  7. centos7 Apache 2.4.6 多域名多网站配置

    Apache 2.4.6 多域名多网站配置 在/etc/httpd/conf 下 编辑 vim httpd.conf 添加:ServerName 外网IP 并注释 #DocumentRoot &quo ...

  8. RESTFUL 设计风格

    RESTFUL  规范总结: Rest是web服务的一种架构风格;使用HTTP,URI,XML,JSON,HTML等广泛流行的标准和协议;轻量级,跨平台,跨语言的架构设计;它是一种设计风格,不是一种标 ...

  9. python catch socket timeout

    python catch socket timeout import socket try: # do something. except socket.timeout as e: # socket ...

  10. jquery接触初级-----ajax 之:load()方法

    jquery _ajax 请求主要有几种方式:load(),$.get(),$.post(),$.ajax(),$.getScript(),$.getJson() 1.load()方法 格式:load ...