非常经典的问题,使用栈来解决,我这里自己实现了一个栈,当然也能够直接用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的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. [LeetCode] Longest Valid Parentheses 解题思路

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. [Leetcode] longest valid parentheses 最长的有效括号

    Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...

  4. [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉

    (Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...

  5. [LeetCode] 20. Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  6. [LeetCode] 20. Valid Parentheses 合法括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  7. [LeetCode] Longest Valid Parentheses

    第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...

  8. [LeetCode] Longest Valid Parentheses 动态规划

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. [leetcode]20. Valid Parentheses有效括号序列

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  10. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

随机推荐

  1. 【自学php】第三天 - 读写文件

    这次的例子是把订单的数据保存起来,一般是用数据库来进行数据的存储最好,但是今天目的是为了学习读写文件,所以这次把数据存在文件里. 读写文件有一般有三个步骤: 1)打开文件.如果文件不存在,需要先创建它 ...

  2. 关于“创业者与VC见面的10个不成文细节点”

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:Will Wang链接:http://www.zhihu.com/question/19641135/answer/50974 ...

  3. node.js 入门教程(beginnder guide

    非常好的教程: node入门: JavaScript与Node.js JavaScript与你 简短申明 服务器端JavaScript “Hello World” 一个完整的基于Node.js的web ...

  4. <!DOCTYPE html>

    html5标准网页声明,原先的是一串很长的字符串,现在是这个简洁形式,支持html5标准的主流浏览器都认识这个声明.表示网页采用html5.

  5. Hibernate、乐观锁和悲观锁

    悲观锁(Pessimistic Lock), 顾名思义,就是很悲观,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会block直到它拿到锁.传统的关系型数据 ...

  6. C# System.Guid.NewGuid() 【转】

    概念 GUID: 即Globally Unique Identifier(全球唯一标识符) 也称作 UUID(Universally Unique IDentifier) . GUID是一个通过特定算 ...

  7. 使用IIS建立自己的网站、使用C#编写IIS模拟器,更好的理解Client和Server的relation

    如何在IIS服务器上搭建自己的网站呢,今天带着这问题进行简单的实践一下,并且准备模拟一下IIS服务器工作方式,把这个工作方式搞清楚有利于发展. 1.首先应该进入控制面板=>程序=>添加或删 ...

  8. sql中在查询语句中加判断,控制输出的内容

    Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...

  9. 连接远程hbase长时间等待问题

    确保本地保存了远程主机名: 保存远程hosts

  10. [转]不用Cookie的“Cookie”技术

    有另外一种比较隐蔽的用户追踪技术,不使用cookie或者Javascript.很多网站已经在用了,但知道的人不多.本文就来介绍一下这种技术是如何追踪用户,用户又该如何避免追踪. 这种技术不依赖于: C ...