leetcode_20题——Valid Parentheses(string,stack堆栈)

Valid Parentheses

Total Accepted: 47887 Total Submissions: 180550My Submissions

Question Solution

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.

Hide Tags

Stack String

Have you met this question in a real interview?

Yes

No

Discuss

#include<iostream>
#include<string>
#include <stack>
using namespace std;

char change_char(char a)
{
	switch(a)
	{
	case ‘(‘:{return ‘)‘;break;}
	case ‘)‘:{return ‘(‘;break;}
	case ‘{‘:{return ‘}‘;break;}
	case ‘}‘:{return ‘{‘;break;}
	case ‘[‘:{return ‘]‘;break;}
	case ‘]‘:{return ‘[‘;break;}
	}
}
/*采用堆栈的方法,在是前括号时将其压入到堆栈中,是反括号时进行匹配
*/
bool isValid(string s) {
	stack<char> str_temp;
	if(s[0]==‘(‘||s[0]==‘[‘||s[0]==‘{‘)
		str_temp.push(s[0]);
	else
		return 0;

	int i=1;
	int len=s.size();
	while(i<len)
	{
		if(s[i]==‘(‘||s[i]==‘[‘||s[i]==‘{‘)//是前括号
		{str_temp.push(s[i]);i++;}
		else//是反括号
		{
			if(str_temp.empty())//栈里为空
				return 0;
			else
			{
				if(str_temp.top()!=change_char(s[i]))//不匹配
					return 0;
				str_temp.pop();
				i++;
			}
		}
	}
	if(str_temp.empty())//栈中是否为空
		return 1;
	else
		return 0;
}
int main()
{
	string s="()";
	cout<<isValid(s)<<endl;
	system("pause");
	return 1;
}

  

时间: 2024-11-07 16:59:27

leetcode_20题——Valid Parentheses(string,stack堆栈)的相关文章

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_20 Valid Parentheses(String)

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第20题--Valid Parentheses

Problem: 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 "

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 "([)]"

【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 example is &

20. Valid Parentheses做题报告

题目链接: Valid Parentheses 题目大意: 判断字符串s的字符是否满足符号匹配 做题报告: (1)该题涉及的算法与数据结构 栈,哈希表 (2)自己的解答思路+代码+分析时间和空间复杂度 思路: 栈先入后出特点,若遇到左括号入栈,遇到右括号时将对应栈顶左括号出栈,则遍历完所有括号后 stack 仍然为空则表示满足符号匹配,输出true,否则输出false 代码: import java.util.Stack; class Solution { public boolean isVa

刷题32. Longest Valid Parentheses

一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过5次提交终于正确了. 性能如下: Runtime: 8 ms, faster than 61.76% of C++ online submissions for Longest Valid Parentheses. Memory Usage: 9.8 MB, less than 10.71% of

leetcode第31题--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 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 example is &