20. Valid Parentheses(stack)

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.

public class Solution {

if (s == null)
			return false;
		Stack<Character> stack = new Stack<Character>();
		char[] ch = s.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			if ((ch[i] == ‘(‘) || (ch[i] == ‘[‘) || (ch[i] == ‘{‘))
				stack.push(ch[i]);
			else {
				if (stack.isEmpty())
					return false;
				if (ch[i] - stack.pop() > 2)
					return false;
			}
		}
		return stack.isEmpty();

  

    public boolean isValid(String s) {
        if (s == null)                       //改进1:可以不用String.charat(i)
			return false;          //改进2: 先判断长度                                              //改进三: 不需要这么多if else ,符号的判断可以用ascii码
		Stack<Character> stack = new Stack<Character>();
		char[] ch = s.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			if ((ch[i] == ‘(‘) || (ch[i] == ‘[‘) || (ch[i] == ‘{‘))
				stack.push(ch[i]);
			else {
				if (ch[i] == ‘)‘) {
					if (stack.isEmpty())
						return false;
					else if (stack.pop() != ‘(‘)
						return false;
				}
				if (ch[i] == ‘]‘) {
					if (stack.isEmpty())
						return false;
					if (stack.pop() != ‘[‘)
						return false;
				}
				if (ch[i] == ‘}‘) {
					if (stack.isEmpty())
						return false;
					if (stack.pop() != ‘{‘)
						return false;
				}
			}
		}
		if (!stack.isEmpty())
			return false;
		else
			return true;
    }
}

  

时间: 2024-10-08 19:25:10

20. Valid Parentheses(stack)的相关文章

LeetCode:20. Valid Parentheses(Easy)

1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')', '{', '}', '[' 和 ']'. 合法:形如"()[]"."{[()]}" 不合法:形如"([)]"."[[((" 判断所给字符串s是否合法. 3. 解题思路 对字符串s转换成字符数字进行遍历. 利用栈,遇到左半边字符:'

20. Valid Parentheses(js)

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

32. Longest Valid Parentheses (Stack; DP)

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

LeetCode 之 Longest Valid Parentheses(栈)

[问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another exa

Valid Parentheses (python)

这道题主要用栈来实现的.什么是栈呢,参照书上的后缀表达式的例子谈谈自己的理解,栈最明显的特征是先进后出.所以可以有效的结合题目中 ()对匹配问题,可以把从列表中获取的符号先存到栈中. 首先建个空列表用于映射栈中元素.然后挨个查询传递过来的列表的每个元素,不在栈中就压进栈,在的话再看看是不是栈顶元素.是的话就把栈顶元素弹出.查询完所有元素,如果栈为空返回true. 刷的第二道题,感觉很费劲啊. 原文地址:https://www.cnblogs.com/shaer/p/9611013.html

leetcode题解:Valid Parentheses(栈的应用-括号匹配)

题目: 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 "([)]&

【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】

[032-Longest Valid Parentheses(最长有效括号)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid paren

LeetCode解题笔记 - 20. Valid Parentheses

这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶段性查看,一定能对自己有帮助. 这是我做的第一题,所以记录也从这题开始,之后尽力以简短的说明,描述出思路,方便以后能回顾到简介明了的记录. 20. Valid Parentheses Given a string containing just the characters '(', ')', '{

20. Valid Parentheses【leetcode】

20. Valid Parentheses 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 "(]&quo