leetcode_20_Valid Parentheses

麻烦各位朋友帮忙顶一下增加人气,如有错误或疑问请留言纠正,谢谢

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

关于堆栈的知识回顾:

#include <stack>

empty() 堆栈为空则返回真

pop() 移除栈顶元素

push() 在栈顶增加元素

size() 返回栈中元素数目

top() 返回栈顶元素

stack<int> a   定义一个堆栈

//vs2012测试代码
#include<iostream>
#include<string>
#include<stack>

using namespace std;

class Solution {
public:
    bool isValid(string s)
	{
		stack<char> temp;
		for(int i=0; i<s.length(); i++)
		{
			if( !temp.empty() )
			{
				if( temp.top()=='(' && s[i]==')'
					|| temp.top()=='[' && s[i]==']'
					|| temp.top()=='{' && s[i]=='}')
						temp.pop();
				else
					temp.push(s[i]);
			}
			else
				temp.push(s[i]);
		}
		return temp.empty();
    }
};

int main()
{
	string s;
	getline( cin , s);  //或者cin>>s;
	Solution lin;
	cout<<lin.isValid(s)<<endl;

	return 0;
}
//方法一:自测Accepted
class Solution {
public:
    bool isValid(string s)
	{
		stack<char> temp;
		for(int i=0; i<s.length(); i++)
		{
			if( !temp.empty() )
			{
				if( temp.top()=='(' && s[i]==')'
					|| temp.top()=='[' && s[i]==']'
					|| temp.top()=='{' && s[i]=='}')
						temp.pop();
				else
					temp.push(s[i]);
			}
			else
				temp.push(s[i]);
		}
		return temp.empty();
    }
};
//方法二:其他人版本
class Solution {
public:
    bool matchBracket(char a, char b)
    {
        if(a == '[' && b == ']') return true;
        if(a == '(' && b == ')') return true;
        if(a == '{' && b == '}') return true;
        return false;
    }
    bool isValid(string s) {
        stack<char> charStack;
        for(int i = 0; i < s.size(); ++i)
        {
            if(charStack.empty() || !matchBracket(charStack.top(), s[i])) charStack.push(s[i]);
            else charStack.pop();
        }

        return charStack.empty();
    }
};
时间: 2024-11-10 11:35:12

leetcode_20_Valid Parentheses的相关文章

leetcode22. Generate Parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 这个问题解的个

Generate Parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" class S

#22 Generate Parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 本题是括号匹配

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

LeetCode 241. Different Ways to Add Parentheses

241. Different Ways to Add Parentheses Add to List Description Submission Solutions Total Accepted: 38849 Total Submissions: 92740 Difficulty: Medium Contributors: Admin Given a string of numbers and operators, return all possible results from comput

leetcode 20 Valid Parentheses

class Solution { public: bool isValid(string s) { stack<char> parentheses; for (int i = 0; i < s.size(); ++i) { if (s[i] == '(' || s[i] == '[' || s[i] == '{') parentheses.push(s[i]); else { if (parentheses.empty()) return false; if (s[i] == ')' &

22. Generate Parentheses——本质:树,DFS求解可能的path

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: [ "((()))", "(()())", "(())()", "()(())", "()()()" ] cla

32. 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 &

241. Different Ways to Add Parentheses

241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parentheses/ 思路就是:首先找到以运算符为根节点,分别计算左子串和右子串的所有结果的集合,然后依次进行组合计算.参考博客http://www.cnblogs.com/ganganloveu/p/4681439.html. 自己的思路错了,直接用两边只用了一个整数去接收左右子串的计算值!! #include