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. python简单的游戏场景代码

    模拟英雄联盟游戏场景的简单场景 最后计算出英雄的战斗力 class Hero: def __init__(self, na, gen, age, fig): self.name = na self.g ...

  2. jeff dean的主页

    https://ai.google/research/people/jeff/ 上面有他的很多论文和ppt 很不错 我于1999年中期加入Google,目前是研究小组的Google高级研究员,负责Go ...

  3. 使用Spring容器最简单的代码姿势

    如果仅仅是为了测试简单使用一下Spring的IOC容器,或者研究一下Spring的容器的源码实现,那么在搭建Spring工程的时候,不需要复杂的xml配置.Spring3.0之后提供了Java注解配置 ...

  4. win10磁盘分区

    注意:未分配的磁盘是无法使用的. 安装系统的时候,我们可以先将系统装起来,再来对未分配的磁盘进行操作. 具体步骤:https://jingyan.baidu.com/article/fcb5aff76 ...

  5. 3秒钟unittest入门使用

    一:unittest跑起来 unittest使用,具体使用方式可以查看unittest官网,以下简单做个介绍,在工作中使用最多的 # 第一步: 创建unittest类,且一定要继承unittest.T ...

  6. [简短问答]SET_PRINT_STYLEA相关简短问答

    常见1:SET_PRINT_STYLEA(0,.....)放在那里参考官网下载中心的LODOP技术手册SET_PRINT_STYLEA篇,0代表前面紧跟着的打印项,放在需要该样式的打印项后面紧跟着. ...

  7. [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  8. [LeetCode] 163. Missing Ranges 缺失区间

    Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...

  9. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  10. 【VS开发】【C/C++开发】memcpy和memmove的区别

    memcpy和memmove()都是C语言中的库函数,在头文件string.h中,作用是拷贝一定长度的内存的内容,原型分别如下: void *memcpy(void *dst, const void ...