转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html

描述

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

An input string is valid if:

Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.

Example 1:

Input: "()"
Output: true
Example 2:

Input: "()[]{}"
Output: true
Example 3:

Input: "(]"
Output: false
Example 4:

Input: "([)]"
Output: false
Example 5:

Input: "{[]}"
Output: true

解析

括号匹配问题。

如果只有一种括号,我们完全可以用一个计数器 count ,遍历整个字符串,遇到左括号加 1 ,遇到右括号减 1,遍历结束后,如果 count 等于 0 ,则表示全部匹配。但如果有多种括号,像 ( [ ) ] 这种情况它依旧会得到 0,所以我们需要用其他的方法。类似大学里面写的计算器。

栈!

遍历整个字符串,遇到左括号就入栈,然后遇到和栈顶对应的右括号就出栈,遍历结束后,如果栈为空,就表示全部匹配。

代码

public boolean isValid(String s) {
Stack<Character> brackets = new Stack<Character>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
switch (c) {
case '(':
case '[':
case '{':
brackets.push(c);
break;
case ')':
if (!brackets.empty()) {
if (brackets.peek() == '(') {
brackets.pop();
} else {
return false;
}
} else {
return false;
}
break;
case ']':
if (!brackets.empty()) {
if (brackets.peek() == '[') {
brackets.pop();
} else {
return false;
}
} else {
return false;
}
break;
case '}':
if (!brackets.empty()) {
if (brackets.peek() == '{') {
brackets.pop();
} else {
return false;
}
} else {
return false;
} }
} return brackets.empty();
}

[LeetCode] 20. Valid Parentheses ☆的更多相关文章

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

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

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

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

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

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

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

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

  5. leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法

    Valid Parentheses  Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...

  6. leetcode 20 Valid Parentheses 括号匹配

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

  7. LeetCode 20 -- Valid Parentheses

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

  8. Java [leetcode 20]Valid Parentheses

    题目描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...

  9. LeetCode 20 Valid Parentheses (括号匹配问题)

    题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description   Problem: 括号匹配问题. 使用栈,先进后出!   ...

随机推荐

  1. layer 弹出层 回调函数调用 弹出层页面 函数

    1.项目中用到layer 弹出层,定义一个公用的窗口,问题来了窗口弹出来了,如何保存页面上的数据呢?疯狂百度之后,有了结果,赶紧记下. 2.自己定义的公共页面方法: layuiWindow: func ...

  2. 【Selenium2】【问题】

    [iframe 和 HTML 相互嵌套] 比如126登录页,我的几个方法都不好用 1. iframeFather = driver.find_element(By.XPATH,"//div[ ...

  3. React组件导入的两种方式(动态导入组件的实现)

    一. react组件两种导入方式 React组件可以通过两种方式导入另一个组件 import(常用) import component from './component' require const ...

  4. vue运行报错--dependency

    ERROR Failed to compile with 1 errors 11:17:27 This dependency was not found: 解决方法:把报错所缺少的依赖都装上 如 xx ...

  5. 基于 Python 和 Pandas 的数据分析(6) --- Joining and Merging

    这一节我们将看一下如何通过 join 和 merge 来合并 dataframe. import pandas as pd df1 = pd.DataFrame({'HPI':[80,85,88,85 ...

  6. ERR! registry error parsing json

    报错日志: ERR! registry error parsing json ERR! registry error parsing json 解决过程: 从github上克隆一个项目,在npm i的 ...

  7. python2.7使用requests时报错SSLError: HTTPSConnectionPool(host='b-ssl.duitang.com', port=443)

    import requests url='https://www.duitang.com/napi/blog/list/by_search/?kw=%E6%A0%A1%E8%8A%B1&sta ...

  8. python 部分函数

    abs(number) ,返回数字的绝对值cmath.sqrt(number) ,返回平方根,也可以应用于负数float(object) ,把字符串和数字转换为浮点数help() ,提供交互式帮助in ...

  9. python 协程 demo

    # -*- coding: UTF- -*- import gevent from gevent import socket from gevent import event rev=socket.s ...

  10. C语言之网络编程(服务器和客户端)

    Linux网络编程 1. 套接字:源IP地址和目的IP地址以及源端口号和目的端口号的组合称为套接字.其用于标识客户端请求的服务器和服务. 常用的TCP/IP协议的3种套接字类型如下所示. (1)流套接 ...