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.

先解释一下:

本题要求:给一个包含“{”,“[”,“(”,")","]","}"  String,判断一下输入的string 结构是否合法; 这些括弧必须遵循正确的规则。

solution:

public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
for (char c : s.toCharArray()) {
if (c == '(')
stack.push(')');
else if (c == '{')
stack.push('}');
else if (c == '[')
stack.push(']');
else if (stack.isEmpty() || stack.pop() != c)
return false;
}
return stack.isEmpty();
}

借着本体的答案讲解一下Stack类;

Stack 为java.util包下类:

构造方法

Stack();创建一个空 Stack。 

E push(E item)
把项压入堆栈顶部。
E pop()
移除堆栈顶部的对象,并作为此函数的值返回该对象。
E peek()
查看堆栈顶部的对象,但不从堆栈中移除它。
boolean empty()
测试堆栈是否为空。
int search(Object o)
返回对象在堆栈中的位置,以 1 为基数。

 

LeetCode算法01 Valid Parentheses的更多相关文章

  1. 【算法】LeetCode算法题-Valid Parentheses

    这是悦乐书的第147次更新,第149篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第6题(顺位题号是20),给定一个只包含字符'(',')','{','}','['和'] ...

  2. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

  3. LeetCode 之 Longest Valid Parentheses(栈)

    [问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...

  4. Java for LeetCode 032 Longest Valid Parentheses

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

  5. Java [leetcode 32]Longest Valid Parentheses

    题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...

  6. [Leetcode][Python]20: Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...

  7. LeetCode算法题-Valid Palindrome II(Java实现)

    这是悦乐书的第287次更新,第304篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第155题(顺位题号是680).给定非空字符串s,最多可以删除一个字符. 判断它是否是回 ...

  8. LeetCode算法题-Valid Perfect Square(Java实现-四种解法)

    这是悦乐书的第209次更新,第221篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第77题(顺位题号是367).给定正整数num,写一个函数,如果num是一个完美的正方形 ...

  9. LeetCode算法题-Valid Anagram(Java实现)

    这是悦乐书的第198次更新,第205篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第61题(顺位题号是242).给定两个字符串s和t,写一个函数来确定t是否是s的anag ...

随机推荐

  1. 自己搭建gitlab服务,组员不能上传代码

    原因是因为  没有拉分支  直接在master 上开撸代码 ,master 分支 默认是受保护的,具体操作如下

  2. phpstudy5.6 No input file specified的解决方法

    一.问题描述 5.6就提示这个错误,切换5.5就可以 二.原因分析 原因1:提示:“No input file specified.”原因在于使用的PHP5.6是fast_cgi模式,而在某些情况下, ...

  3. mariadb 10.2/mysql 8.0实现递归

    借助mysql 8.0的cte(它是iso sql标准的一部分),可以实现递归,mariadb 10.2.2开始支持递归cte,如下: +----+----------+--------------+ ...

  4. 关于idea跳过错误编译的理解, 跳过报错的代码启动项目去debug测试其他正常的代码

    关于idea跳过错误编译的理解 2018年07月13日 19:06:32 weixin_39669410 阅读数 1296   其实idea使用eclipse编译器可以实现跳过报错的代码启动项目去de ...

  5. 开发日记:常用BAT批处理

    备份文件:BackupSourceCode.bat ::自动备份当前文件夹 ::by luomg, 21:15 2010-10-13 ::minguiluo@163.com @echo off tit ...

  6. Archer和ArcherUI配置说明

    如果Bladex的网关端口是80,则需要修改Archer服务端口,并修改ArcherUI的vue.config.js的端口

  7. C#生成Guid的几种方式

    1 var uuid = Guid.NewGuid().ToString(); // 9af7f46a-ea52-4aa3-b8c3-9fd484c2af12 2  var uuidN = Guid. ...

  8. 安裝TA-Lib到想要罵髒話

    收集下載股票交易指數的歷史數據,並計算主要的幾個指標來進行技術分析. 查找網上的資料,發現大家都在用TA-Lib來計算指標,於是在下載並匯入了數據後開始安裝這個庫. pip install TA-Li ...

  9. [LeetCode] 142. Linked List Cycle II 链表中的环 II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  10. GitLab - 安装并启动GitLab

    1 - GitLab安装 1.1 信息确认 [Anliven@node102 ~]$ uname -a Linux node102 3.10.0-957.el7.x86_64 #1 SMP Thu N ...