【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 ...
随机推荐
- [转]Hibernate中property-ref的应用,常用来解决遗留数据库One To Many关系
[转]Hibernate中property-ref的使用,常用来解决遗留数据库One To Many关系 1.如表Class(ClassID,Class_No,ClassName)与Student(S ...
- Web学习
http://book.2cto.com/201309/31936.html http://alvinalexander.com/ 查看锁表进程SQL语句1: select sess.sid, ...
- mysql_healthly
cat mysql_healthly.php <?php if (!defined('IN_PDK')){ define('IN_PDK', true); } $db_name = $_GET[ ...
- 如何用ATL创建ActiveX控件
演示截图: 代码简介或代码解析: 如何用ATL创建ActiveX控件 实现了一个ActiveX控件,它在一个圆内部有个正多边形,当用户在多变形内部单击将会使多边形的边数在当前的基础上+1,在多变形外部 ...
- sql 多条件查询 拼接字符串 改成 普通查询格式
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER PROC [dbo].[usp_SRV_CheckServiceDemandOrder] ) = ...
- 04737_C++程序设计_第1章_认识C++的对象
例1.1 演示使用结构对象的示例程序. //功能:将结构对象的两个域值相加,乘以2再加50 #include <iostream>//包含头文件 using namespace std;/ ...
- The encryption certificate of the relying party trust identified by thumbprint is not valid
CRM2013部署完ADFS后通过url在浏览器中訪问測试是否成功,成功进入登陆界面但在登陆界面输入username和password后始终报身份验证失败,系统中的报错信息例如以下:Microsoft ...
- js 判断字符串中是否有某字符串
<script> var test=['<div class="cur"></div>','<div class="cur&qu ...
- Caused by: java.lang.UnsupportedClassVersionError: com/zy/example/domain/Student : Unsupported major.minor version 51.0
JVM的版本比jdk的版本老,即JVM的版本低于jdk的版本.换个新版本的就可以解决问题.
- 通过url给action传中文参数乱码解决方案
比如: http://localhost:8080/projectName/dutyCondition.action?admitstate=0¤tStep=我的博客 传到后台的时候 ...