【leetcode系列】Valid Parentheses
非常经典的问题,使用栈来解决,我这里自己实现了一个栈,当然也能够直接用java自带的Stack类。
自己实现的栈代码:
import java.util.LinkedList; class StackOne {
LinkedList<Object> data;
int top;
int maxSize; StackOne(int size) {
// TODO Auto-generated constructor stub
top = -1;
maxSize = 100;
data = new LinkedList<Object>();
} int getElementCount() {
return data.size();
} boolean isEmpty() {
return top == -1;
} boolean isFull() {
return top + 1 == maxSize;
} boolean push(Object object) throws Exception {
if (isFull()) {
throw new Exception("栈满");
}
data.addLast(object);
top++;
return true;
} Object pop() throws Exception {
if (isEmpty()) {
throw new Exception("栈空");
}
top--;
return data.removeLast();
} Object peek() {
return data.getLast();
}
}
推断输出是否有效:
public class Solution { public static boolean isValid(String in) {
StackOne stackOne = new StackOne(100);
boolean result = false;
char[] inArray = in.toCharArray();
for (char i : inArray) {
if (i == '(' || i == '[' || i == '{') {
try {
stackOne.push(i);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
continue;
}
if (i == ')') {
if (stackOne.isEmpty()) {
result = false;
} else {
char tmp = '\u0000';
try {
tmp = (Character) stackOne.pop();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (tmp == '(') {
result = true;
}
}
}
if (i == ']') {
if (stackOne.isEmpty()) {
result = false;
} else {
char tmp = '\u0000';
try {
tmp = (Character) stackOne.pop();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (tmp == '[') {
result = true;
}
}
}
if (i == '}') {
if (stackOne.isEmpty()) {
result = false;
} else {
char tmp = '\u0000';
try {
tmp = (Character) stackOne.pop();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (tmp == '{') {
result = true;
}
} }
}
if (!stackOne.isEmpty()) {
result = false;
}
return result;
} public static void main(String[] args) {
System.out.print(isValid("(}"));
}
}
【leetcode系列】Valid Parentheses的更多相关文章
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses 解题思路
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉
(Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] Longest Valid Parentheses
第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...
- [LeetCode] Longest Valid Parentheses 动态规划
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
随机推荐
- FileProvider是个什么东西?
FileProvider是个什么东西? 在<读取并监控文件的变化>中,我们通过三个简单的实例演示从编程的角度对文件系统做了初步的体验,接下来我们继续从设计的角度来继续认识它.这个抽象的文件 ...
- Hibernate防止SQL注入
如果在查询字段中输入单引号"'",则会报错,这是因为输入的单引号和其他的sql组合在一起编程了一个新的sql,实际上这就是SQL注入漏洞,后来我在前台和后台都对输入的字符进行了判断 ...
- Oracle EBS-SQL (PO-8):检查有供货比例无采购员.sql
select distinct msr.sourcing_rule_name 名称 , msi.description 说明 , ...
- Cortex-M3和Cortex-M4 Fault异常应用之二 ----- Fault处理函数的实现
在项目处于调试期间,Fault处理程序可能只是一个断点指令,调试器遇到这个指令后停止程序的运行.默认情况下,由于非硬Fault被禁能,所有发生的非Fault都会上访成硬Fault,因此只要在硬Faul ...
- Python字符串与数字拼接
Python不像JS或者PHP这种弱类型语言里在字符串连接时会自动转换类型,而是直接报错.要解决这个方法只有提前把int转成string,然后再拼接字符串即可. 如代码: 1 2 3 4 5 # co ...
- Linux学习笔记2:如何快速的学习使用一个命令
Linux 分层 内核 库: .so 共享对象,windows:dll 动态链接库 应用程序 Linux的基本原则: 1.由目的单一的小程序组成:组合小程序完成复杂任务: 2.一切皆文件: 3.尽量避 ...
- js中__proto__(内部原型)和prototype(构造器原型)的关系
一.所有构造器/函数的__proto__都指向Function.prototype,它是一个空函数(Empty function) Number.__proto__ === Function.prot ...
- java enum的用法
原始的常量定义: public static fianl MON=“Mon”; public static final TUE="Tue"; 语法(定义) 创建枚举类型要使用 en ...
- http keepalive and tcpkeepalive
http keepalive 减少tcp 连接 (三次握手的消耗) tcp keepalive 检测死链接的 session 在tcp连接中, src_ip + src_port + dest_ip ...
- Tomcat 设置为服务使用脚本 service
进入到Tomcat的bin目录下,如果使用的是Windows系统则使用service.bat进行操作;Linux系统则使用service.sh进行. service.bat install/remov ...