Valid parentheses
problem ' s Link
----------------------------------------------------------------------------
Mean:
Given a sequence of parentheses, check that the parentheses match in order.
Analyse:
The basic application of the stack structure.
Time complexity:o (N)
View Code
/**
* -----------------------------------------------------------------
* Copyright (c) crazyacking. All rights reserved.
* -----------------------------------------------------------------
* author:crazyacking
* date:2016-02-17-16.33
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace STD;
typedef Long Long(LL);
typedef unsigned Long Long(ULL);
Const Double EPS(1e-8);
class Solution
{
Public:
BOOL IsValid(stringS
{
Stack<Char> STA;
for(Auto P:S
{
if(p==' ('|| P==' {'|| P==' [')
STA.Push(p);
Else if(p==' ) ')
{
if(STA.Empty())return false;
Char TP=STA.Top();
STA.Pop();
if(TP!=' (')
return false;
}
Else if(p=='} ')
{
if(STA.Empty())return false;
Char TP=STA.Top();
STA.Pop();
if(TP!=' {')
return false;
}
Else
{
if(STA.Empty())return false;
Char TP=STA.Top();
STA.Pop();
if(TP!=' [')
return false;
}
}
if(STA.Empty())
return true;
Else return false;
}
};
int Main()
{
Solution Solution;
stringS
while(Cin>>S
{
if(Solution.IsValid(s))
cout<<"Yes."<<Endl;
Else cout<<"No."<<Endl;
}
return 0;
}
/*
*/
LeetCode-20. Valid parentheses