Stack String

Description:

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.

堆栈的典型例子

my Solution:

public class Solution {
public boolean isValid(String s) {
Deque<Character> stack = new ArrayDeque<>();
if (s.length() == 0)
return true;
else {
for (int i = 0; i < s.length(); i++) {
Character ch = s.charAt(i);
if (!stack.isEmpty()) {
Character temp = stack.peek();
if ((temp == '(' && ch == ')') || (temp == '[' && ch == ']') || (temp == '{' && ch == '}')) {
stack.pop();
} else {
stack.push(ch);
}
} else {
stack.push(ch);
}
}
}
return stack.isEmpty();
}
}

LeetCode & Q20-Valid Parentheses-Easy的更多相关文章

  1. [leetcode] 20. Valid Parentheses (easy)

    原题链接 匹配括号 思路: 用栈,遍历过程中,匹配的成对出栈:结束后,栈空则对,栈非空则错. Runtime: 4 ms, faster than 99.94% of Java class Solut ...

  2. [LeetCode] 036. Valid Sudoku (Easy) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...

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

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

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

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

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

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

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

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

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

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

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

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

  9. [LeetCode] Longest Valid Parentheses

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

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

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

随机推荐

  1. 号称了解mesos双层调度的你,先来回答下面这五个问题!

    一提mesos,很多人知道双层调度,但是大多数理解都在表面,不然试一下下面五个问题. 问题一:如果有两个framework,一万个节点,按说应该平均分配给两个framework,怎么个分法?一人一台这 ...

  2. 关系型数据库工作原理-归并排序(翻译自Coding-Geek文章)

    本文翻译自Coding-Geek文章:< How does a relational database work>. 原文链接:http://coding-geek.com/how-dat ...

  3. python全栈开发-Day7 文件处理

    python全栈开发-Day7   文件处理 一 .文件操作 一 .介绍 计算机系统分为:计算机硬件,操作系统,应用程序三部分. 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必 ...

  4. 面向对象写的简单的colors rain

      <!DOCTYPE html>   <html>   <head>   <meta charset="UTF-8">   < ...

  5. Gauge----自动化测试工具--使用

    开始吧 1 下载安装gauge(根据官网教程 http://getgauge.io/documentation/user/current/)测试:gauge -v step01 磁盘上新建一个空目录- ...

  6. vue实现懒加载的几种方法

    vue实现惰性加载是基于: 1.ES6的异步机制 components: { comp: (resolve, reject) => {} } 2. webpack的代码分割功能 require. ...

  7. Javscript的函数链式调用基础篇

    我们都很熟悉jQuery了,只能jQuery中一种非常牛逼的写法叫链式操作: $('#div').css('background','#ccc').removeClass('box').stop(). ...

  8. 打印机驱动冲突和端口异常:win10更新部分补丁后,打印机本地连接(连接打印机的主机)可以打印,其他共享网络中的电脑可以连接到打印机,但不能打印——解决方案

    一.问题描述: 1.A(WIN10系统)表示连接打印机的电脑,P表示打印机(型号:惠普127M),B(WIN7系统)表示局域网中的电脑 2.A升级后部分补丁后,A可以使用打印机P打印文件,B显示可以连 ...

  9. Spring Cloud简介以及版本选择

    什么是SpringCloud 官方的说法就是spring Cloud 给开发者提供一套按照一定套路快速开发 分布式系统 的工具. 具体点就是Spring boot实现的微服务架构开发工具.它为微服务架 ...

  10. java中有关流操作的类和接口

    一.java操作l流有关的类和接口 1.File 文件类 2.RandomAccessFile 随机存储文件类 3.InputStream 字节输入流 4.OutputStream 字节输出流 5.R ...