题目

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.

这道题比较经典了,就是有点像编译器判断代码符号是否符合规则,是堆栈的一个简单应用。当遇到"{","[","("的时候入栈,如果遇到这些符号的另一半,则取栈顶比较,如果是一对就继续,不然返回false。

代码

public class Solution {public boolean isValid(String s) {if(s.length()==0) return true;int len=s.length();char[] symbolFirst={'(','{','['};char[] symbolSecond={')','}',']'};char strChar[]=s.toCharArray();     Stack sym=new Stack();for(int i=0;i<len;i++){for(int j=0;j<symbolFirst.length;j++){if(strChar[i]==symbolFirst[j]){if(len==1){return false;}sym.push(strChar[i]);}               }for(int k=0;k<symbolSecond.length;k++){if(strChar[i]==symbolSecond[k]){if(sym.isEmpty()){return false;}else{if(!sym.peek().equals(symbolFirst[k])){return false;   }else{sym.pop();}}}}}if(sym.isEmpty()){return true;}else{return false;}   }
}
代码下载:https://github.com/jimenbian/GarvinLeetCode

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

【LeetCode从零单排】No20.ValidParentheses相关推荐

  1. 【LeetCode从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用)

    1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a stree ...

  2. 【LeetCode从零单排】No70.ClimbingStairs

    题目 爬楼梯问题,这是一道很有趣的问题.首先看题目: You are climbing a stair case. It takes n steps to reach to the top. Each ...

  3. 【LeetCode从零单排】No26.Remove Duplicates from Sorted Array

    题目      题目要求:去除sort int数组中的重复项. Given a sorted array, remove the duplicates in place such that each ...

  4. 【LeetCode从零单排】No.7 Reverse Integer

    前话       今天开始励志刷一下leetcode上面的题目(还好这个网站没被TG和谐).从easy的开始,数一下差不多有40道,争取两个月搞定. 题目        没想到做的第一道题目,虽然看似 ...

  5. 【LeetCode从零单排】No.135Candy(双向动态规划)

    1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  6. 【LeetCode从零单排】No22.Generate Parentheses

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

  7. 【LeetCode从零单排】No221.Maximal Square

    题目 Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  8. 【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)

    背景 (以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool) DFS(Dpeth-first Se ...

  9. 【LeetCode从零单排】No121 Best Time to Buy and Sell Stock

    题目 Say you have an array for which the ith element is the price of a given stock on day i. If you we ...

最新文章

  1. 【H5】解决ios禁止缩放失效的方法
  2. python查看文件夹文件的所有权限,Python判断某个用户对某个文件的权限
  3. WCF分布式开发常见错误(26):Authentication failed
  4. 天猫总架构师何崚:好的技术团队不是“需求翻译机”或“架构优化机”
  5. 使用命令行结合jq提取出Kubernetes配置文件里的secret信息
  6. css 两行字变成一行,如何通过CSS将两行文本更改为一行?
  7. 6. OD-去除收费软件次数限制,去除退出软件时弹出的广告(比如可执行5次)
  8. Web服务器常用设置
  9. 617.合并二叉树(力扣leetcode) 博主可答疑该问题
  10. 基于ssh人事考勤管理系统
  11. mysql商品表_MySQL数据库创建商品表
  12. 需要一款管理链接的软件
  13. [cocos2d-x]SpaceFighting[安卓/IOS都适用]
  14. Unity实现飞机大战简单易懂
  15. 蜗蜗的毛衣花样c++
  16. 一代王者回归?滴滴暂停563天,恢复新用户下载,江湖格局又生变
  17. CSS高度自适应问题
  18. 2019写给对象的话_最新写给女朋友的情话短句 一句话形容最好的爱情说说2019
  19. 功能篇------android 实现“摇一摇”功能
  20. 一手掌控美食新鲜“灵魂”且融汇到生活的方方面面

热门文章

  1. 吴恩达第三周逻辑回归
  2. linux安装mysql5.7.18_Linux 安装MySQL5.7.18
  3. 理想内插_理想ONE性能测试:6.35秒就能破百,售价只要宝马X5一半
  4. mysql utf8 乱码_MySql UTF8乱码问题应这样修改
  5. 计算机未来的储存装置,图说计算机存储设备
  6. #error使用分析
  7. C、C++中的转义符
  8. java 自带观察者模式_java 内置的观察者模式
  9. Android:Margin和Padding
  10. sklearn.preprocessing.PolynomialFeatures