0020_Valid Parentheses
来源:互联网 发布:网络计算机培训 编辑:程序博客网 时间:2024/05/01 12:24
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.
JAVA
直接使用栈来做,不过之前在看书的时候,好像说java中的栈被取代了???记不清了,这次先用栈做一下,下次去书中找一下看看是不是自己记错了。时间复杂度
public class Solution { public boolean isValid(String s) { if(s.length() % 2 != 0){ return false; } Stack<Character> sta = new Stack<Character>(); char temp; for(int i = 0; i < s.length();++i){ temp = s.charAt(i); if(sta.empty()){ sta.push(temp); }else { switch (temp){ case '(': case '[': case '{': sta.push(temp); break; case ')': if(sta.peek() == '('){ sta.pop(); }else{ return false; } break; case ']': if(sta.peek() == '['){ sta.pop(); }else{ return false; } break; case '}': if(sta.peek() == '{'){ sta.pop(); }else{ return false; } break; } } } if(sta.empty()){ return true; } return false; }}
阅读全文
0 0
- 0020_Valid Parentheses
- 【LeetCode】20_Valid Parentheses
- leetcode 019_Valid Parentheses
- LeetCode 20_Valid Parentheses
- [LeetCode]20_Valid Parentheses
- leetcode第一刷_Valid Parentheses
- 20_Valid Parentheses徒手尝试#2(Done)
- 65_Valid Number
- 【LeetCode】125_Valid Palindrome
- 0036_Valid Sudoku
- leetcode第一刷_Valid Palindrome
- leetcode第一刷_Valid Number
- leetcode第一刷_Valid Sudoku
- Generate Parentheses & Valid Parentheses
- Parentheses Balance
- Generate Parentheses
- Valid Parentheses
- Generate Parentheses
- Mysql主从复制原理解析和技术难点解决
- nginx 学习笔记(三)基本数据结构
- 考研英语
- [置顶] Android APK反编译就这么简单 详解(附图)
- Android开发之PathMeasure的基本用法
- 0020_Valid Parentheses
- Thymeleaf系列五 迭代,if,switch语法
- Android7.0权限适配
- python 格式化输出
- 使用脚本进行同行股票基本面对比效果
- python 相对导入
- dubbo 部分 配置的关系-dubbo github 官方案例
- Linux相关挂载操作
- PAT乙级真题及训练集(2)--1011. A+B和C (15)