题目:
1.Valid Parentheses
括号是否匹配
2.Longest Valid Parentheses
最长的有效匹配
3.Evaluate Reverse Polish Notation
逆波兰表示法进行求值
1.熟练STL栈的基本操作
#include <stack>
using namespace std;
stack <char> s;
s.push(a);
s.pop();
a=s.top();
s.empty();
s.size() ;
2.自己实现栈
1)定义
typedef struct stack_t {
int size; /** 实际元素个数 */
int capacity; /** 容量,以元素为单位 */
stack_elem_t *elems; /** 栈的数组 */
}stack_t;
size会用来做为栈数组操作的下标
2).操作
typedef int stack_elem_t; // 元素的类型
初始化:
指定容量和malloc
void stack_init(stack_t *s, const int capacity) {
s->size = 0;
s->capacity = capacity;
s->elems = (stack_elem_t*)malloc(capacity * sizeof(stack_elem_t));
}
释放
void stack_uninit(stack_t *s) {
s->size = 0;
s->capacity = 0;
free(s->elems);
s->elems = NULL;
}
进栈:
在栈满的情况下,需要先申请空间
void stack_push(stack_t *s, const stack_elem_t x)
{
if(s->size == s->capacity) { /* 已满,重新分配内存 */
stack_elem_t* tmp = (stack_elem_t*)realloc(s->elems, s->capacity * 2 * sizeof(stack_elem_t));
s->capacity *= 2;
s->elems = tmp;
}
s->
elems[
s->size++] = x;
}
出栈
void stack_pop(stack_t *s) {
s->size--;
}
栈顶元素
stack_elem_t stack_top(const stack_t *s) {
return s->elems[
s->size - 1];
}
3.用栈解决问题的思路
前序遍历:
4.题目汇总
描述
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.
分析:
1.用栈解决
class Solution {
public:
bool isValid(string s) {
if(s.size()<=1)
return false;
stack<char> sta;
int i=0,len=s.size();
sta.push(s[i++]);
while(i<len)
{
/* []) */
if (sta.empty()) {
sta.push(s[i++]);
continue;
}
if( s[i] == '(' || s[i] == '[' || s[i] == '{' )
sta.push(s[i++]);
else
{
if( check(sta.top(),s[i] ) )
{
sta.pop();
i++;//少了这一个,编译了很多次
continue;
}
else{
return false;
}
}
}
if(sta.empty())//这个条件也不要忘了
return true;
else
return false;
}
private:
bool check(char a,char b)
{
if (a == '(' and b == ')') return true;
if (a == '[' and b == ']') return true;
if (a == '{' and b == '}') return true;
return false;
}
};
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +
, -
, *
, /
. Each operand may be an integer or another expression.
Some examples:
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
分析:
class Solution {
public:
int evalRPN(vector<string> &tokens) {
int ans=0;
stack<int> s;
int a,b,c;
for(int i=0;i<tokens.size();i++)
{
if(tokens[i]!="+"&&tokens[i]!="-"&&tokens[i]!="*"&&tokens[i]!="/")
{
s.push(stoi(tokens[i]));
}
else
{
a=s.top();
s.pop();
b=s.top();
s.pop();
if(tokens[i]=="+") c=b+a;
if(tokens[i]=="-") c=b-a;
if(tokens[i]=="*") c=b*a;
if(tokens[i]=="/") c=b/a;
s.push(c);
}
}
return s.top();
}
private:
int stoi(string s)
{
/*考虑负数,flag=1*/
int flag=0;
if(s[0]=='-') flag=1;
int i= (flag==0)?0:1;//负数的话,i 从1开始
int len=s.size();
int num=0;
for(;i<len;i++)
{
num=num*10+(s[i]-'0');
}
return (flag==0)?num:(-num);
}
};
描述
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 ”)()())”, where the longest valid parentheses substring is ”()()”, which has
length = 4
分析:
1.可以用栈,也可以用动态规划