[leetcode]_Valid Parentheses
题目:判断给定字符串中的括号是否合法。题目中涉及三种符号'(' + ')' , '[' + ']' , '{' + '}'。
思路:利用stack来存储符号。
注意申请char型stack是: Stack<Character> op = new Stack<Character>();
stack.peek() 查看栈顶元素,但是不弹出;stack.pop() 查看栈顶元素,并弹出。
解题:
public boolean isValid(String s) {
int len = s.length();
Stack<Character> op = new Stack<Character>();
for(int i = 0 ; i < len ; i++){
char cur = s.charAt(i);
if(!op.isEmpty()){
char top = op.peek();
if(top == '(' && cur == ')' || top == '{' && cur == '}' || top == '[' && cur == ']') op.pop();
else op.push(cur);
}else{
op.push(cur);
}
}
return op.isEmpty();
}
轻松AC,咻~
[leetcode]_Valid Parentheses的更多相关文章
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- LeetCode: Valid Parentheses 解题报告
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...
- LeetCode Generate Parentheses (DFS)
题意 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- LeetCode: Generate Parentheses 解题报告
Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...
- LeetCode——Generate Parentheses
Description: Given n pairs of parentheses, write a function to generate all combinations of well-for ...
- [LeetCode]Generate Parentheses题解
Generate Parentheses: Given n pairs of parentheses, write a function to generate all combinations of ...
- [Leetcode] valid parentheses 有效括号对
Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...
随机推荐
- 织梦后台更新,报错DedeCMS Error:Tag disabled:"php" more...
网站采用织梦v5.7版本,在做过一次后台补丁更新后,再对网站“生成”操作的时候,无厘头出现报错“ 网站后台--系统--系统基本参数---其他选项 ---模板引擎禁用标签:php ,把php删掉 保存 ...
- [Java] 对象排序示例
package test.collections; import java.util.ArrayList; import java.util.Collection; import java.util. ...
- shell export
export命令将使系统在创建每一个新的shell时定义这个变量的一个拷贝.这个过程称之为变量输出. 在脚本中export,跟在终端export原理一样. 他们都是一个子shell. http://b ...
- selenium启动firefox、ie、chrome各浏览器方法
1.启动firefox浏览器 a.如果你的本地firefox是默认路径安装的话,如下方式即可启动浏览器 WebDriver driver = new FirefoxDriver(); driver.g ...
- C Primer Plus(第五版)7
第 7 章 C 控制语句:分支和跳转 在本章中你将学习下列内容: · 关键字:if(如果),else(否则),switch(切换),continue(继续),break(中断), case(情况),d ...
- C++primer 练习15.15
// 15_15.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- linux下udp编程
#include<sys/types.h> #include<sys/socket.h> #include<unistd.h> #include<netine ...
- php读取目录及子目录下所有文件名的方法
本文实例讲述了php读取目录及子目录下所有文件名的方法,分享给大家供大家参考.具体实现方法如下: 一般来说php中读取目录下的文件名的方式确实不少,最简单的是scandir,具体代码如下: $dir= ...
- Redis多机功能之复制
复制的目的:创建具有相同数据库的拷贝服务器:扩展系统处理读请求的能力: 复制的定义 Redis的复制(replication)功能允许用户根据一个Redis服务器来创建任意多个该服务器的复制品,其中被 ...
- Windows下cwRsync搭建步骤
文章(一) CwRsync是基于cygwin平台的rsync软件包,支持windows对windows.windows对Linux.Linux对windows高效文件同步.由于CwRsync已经集成了 ...