【Leetcode-easy】Valid Parentheses
思路:读取字符串中的每一个字符,并与栈顶元素进行比较,如果匹配则栈顶元素出栈,否则进栈。直到全部元素都出栈,否则该括号字符串不合法。需要注意细节。
public boolean isValid(String s) {
Stack<Character> stack=new Stack<Character>();
for(int i=0;i<s.length();i++){
char t=s.charAt(i);
if(t=='(' || t=='[' || t=='{'){
stack.push(t);
}else if(!stack.isEmpty()&&(t==')'&&stack.peek()=='(' || t==']'&&stack.peek()=='[' || t=='}'&&stack.peek()=='{')){
stack.pop();
}else{
return false;
}
}
if(stack.isEmpty()){
return true;
}else{
return false;
}
}
【Leetcode-easy】Valid Parentheses的更多相关文章
- 【leetcode系列】Valid Parentheses
非常经典的问题,使用栈来解决,我这里自己实现了一个栈,当然也能够直接用java自带的Stack类. 自己实现的栈代码: import java.util.LinkedList; class Stack ...
- C# 写 LeetCode easy #20 Valid Parentheses
20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- 【Leetcode】【Easy】Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 【LeetCode OJ】Valid Palindrome
Problem Link: http://oj.leetcode.com/problems/valid-palindrome/ The following two conditions would s ...
- 【LeetCode练习题】Valid Palindrome
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- 【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 【LeetCode练习题】Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- 【LeetCode题解】136_只出现一次的数字
目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...
随机推荐
- 在网页里插入flash的代码
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...
- js 模拟发短信
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- JAVA Eclipse开发Android如何让屏幕保持为竖直或水平状态
在Manifest.xml文件中找到activity部分,添加下面这一行 android:screenOrientation="landscape" landscape是横向,po ...
- C 错误处理
C 错误处理 C 语言不提供对错误处理的直接支持,但是作为一种系统编程语言,它以返回值的形式允许您访问底层数据.在发生错误时,大多数的 C 或 UNIX 函数调用返回 1 或 NULL,同时会设置一个 ...
- FiddlerScript学习一:改动Request或Response
前两天因项目须要,简单看了一下FiddlerScript,功能挺强的.今天有时间细致看一下,做个笔记. 改动Request或Response 改动Request和Response要在FiddlerSc ...
- vue v-model使用说明
1.概述 v-model 会忽略所有表单元素的 value.checked.selected 特性的初始值而总是将 Vue 实例的数据作为数据来源.你应该通过 JavaScript 在组件的 data ...
- js:argument
引用:http://www.cnblogs.com/lwbqqyumidi/archive/2012/12/03/2799833.html http://www.cnblogs.com/Fskj ...
- MySQL -进阶
一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用 SELECT * FROM(SELE ...
- svn自动部署
版本库目录hooks下创建post-commit.bat TortoiseProc.exe /command:update /path:"E:\web_server\sial\" ...
- UML类图简明教程
作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells Github:https://github.co ...