Leetcod--20. Valid Parentheses(极简洁的括号匹配)
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 正常的代码
class Solution {
public boolean isValid(String s) {
Stack<Character> st=new Stack<Character>();
for(char c:s.toCharArray()){
if(c=='('||c=='['||c=='{')
st.push(c);
else if(c=='}'&&!st.empty()&&st.peek()=='{')
st.pop();
else if(c==']'&&!st.empty()&&st.peek()=='[')
st.pop();
else if(c==')'&&!st.empty()&&st.peek()=='(')
st.pop();
else
return false;
}
return st.empty();
}
}
很巧妙的方法
class Solution {
public boolean isValid(String s) {
Stack<Character> st=new Stack<Character>();
for(char c:s.toCharArray()){
if(c=='(')
st.push(')');
else if(c=='[')
st.push(']');
else if(c=='{')
st.push('}');
else if(st.empty()||st.pop()!=c)
return false;
}
if(st.empty())
return true;
else
return false;
}
}
Leetcod--20. Valid Parentheses(极简洁的括号匹配)的更多相关文章
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 20. Valid Parentheses[E]有效的括号
题目 Given a string containing just the characters '(',')','[',']','{' and '}',determine if the input ...
- 32. Longest Valid Parentheses(最长括号匹配,hard)
Given a string containing just the characters '(' and ')', find the length of the longest valid (w ...
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- leetCode练题——20. Valid Parentheses
1.题目 20. Valid Parentheses——Easy Given a string containing just the characters '(', ')', '{', '}', ...
- 刷题20. Valid Parentheses
一.题目说明 这个题目是20. Valid Parentheses,简单来说就是括号匹配.在学数据结构的时候,用栈可以解决.题目难度是Medium. 二.我的解答 栈涉及的内容不多,push.pop. ...
- 20. Valid Parentheses【leetcode】
20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
随机推荐
- vue 初识组件
Vue.component("greeting",{ template: `<p>{{ name }}大家好 <button v-on:click="c ...
- django项目创建启动 ORM操作
. HTTP协议消息的格式: . 请求(request) 请求方法 路径 HTTP/1.1\r\n k1:v1\r\n ...\r\n \r\n 请求体 <-- 可以有,可以没有 . 响应(re ...
- C++中的npos,size_t,size_type
string类提供了6种查找函数,每种函数以不同形式find命名,这些操作全都返回string::size_type类型的值,以下标形式标记查找匹配所发生的位置,或返回一个名为string::npos ...
- [Robot Framework] 通过SikuliLibrary可以获取到图片,但是点击失效
执行时,可以看到鼠标已经移动到图片上了,但是点击失效,日志也没有报错 后来发现是windows权限的问题. 通过打开Control Panel->System and Security-> ...
- JavaScript调用wcf服务,并且处理返回的字典集合
1.第一步创建wcf服务的方法 using System;using System.Collections.Generic;using System.Linq;using System.Runtime ...
- MVVM模式理解
MVVM 是Model-View-ViewModel 的缩写,它是一种基于前端开发的架构模式,其核心是提供对View 和 ViewModel 的双向数据绑定,这使得ViewModel 的状态改变可以自 ...
- vue动态路由配置,vue路由传参
动态路由: 当我们很多个页面或者组件都要被很多次重复利用的时候,我们的路由都指向同一个组件,这时候从不同组件进入一个"共用"的组件,并且还要传参数,渲染不同的数据 这就要用到动态路 ...
- vue中常用的指令
1. v-textv-text主要用来更新textContent,可以等同于JS的text属性. <span v-text="msg"></span> 2. ...
- Hadoop知识点
1.小文件合并:如果文件有一定的规律或者是在同一个文件夹下,可以采用获取文件夹下所有的文件,通过流进行合并,然后再存到hdfs上. 2.mapreduce的优点:1.离线计算.2.高容错性,一个节点挂 ...
- system v 共享内存
#include <stdio.h> #include <string.h> #include <errno.h> #include <unistd.h> ...