我的个人博客
Leetcode题目 20
![image-20210625172525394]()
执行结果
![image-20210625172657711]()
Solution题解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
class Solution { public boolean isValid(String s){
Stack<Character> stack = new Stack<>(); 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(); }
public static void main(String[] args){
System.out.println((new Solution()).isValid("()[]{}")); System.out.println((new Solution()).isValid("(])]{}")); } }
|
FROM:gylq.gitee Author:孤桜懶契
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
点赞
https://cn-sec.com/archives/729939.html
复制链接
复制链接
-
左青龙
- 微信扫一扫
-
-
右白虎
- 微信扫一扫
-
评论