bool isValid(string s) {
stack<char> st;
for (int i=0;i<s.size();i++){
if ((s[i]=='(') ||(s[i]=='[') ||(s[i]=='{')) st.push(s[i]);
else{
if (st.empty()) returnfalse;
if ((s[i]==')') && (st.top()!='(')) returnfalse;
if ((s[i]=='}') && (st.top()!='{')) returnfalse;
if ((s[i]==']') && (st.top()!='[')) returnfalse;
st.pop();
}
}
return st.empty();
}
Basic Calculator
int calculate(string s){
int len=s.lenght(),res=0,sign=1;
stack<int> st;
for(int i=0;i<len;i++){
if(isdigit(s[i])){
int sum=s[i]-'0';
while(i+1<len && isdigit(s[i+!])){
sum+=sum*10+s[i+1]-'0';
i++;
}
res+=sum*sign;
}
elseif(s[i]=='+') sign=1;
elseif(s[i]=='-') sign=-1;
elseif(s[i]=='('){
st.push(res);
st.push(sign);
res=0;
sign=1;
}
elseif(s[i]==')'){
int t=st.top();st.pop();
int p=st.top();st.pop();
res=res*t+p;
}
}
return res;
}
Implement Queue using Stacks
/*Implement the following operations of a stack using queues.
* push(x) -- Push element x onto stack.
* pop() -- Removes the element on top of the stack.
* top() -- Get the top element.
* empty() -- Return whether the stack is empty.*/class Stack {
private:
queue<int> que;
public:
// Push element x onto stack.void push(int x) {
que.push(x);
for(int i=0;i<que.size()-1;++i){
//不能使用i<=que.size()-2,OJ提示为TLE,思考中.....
que.push(que.front());
que.pop();
}
}
// Removes the element on top of the stack.void pop() {
que.pop();
}
// Get the top element.int top() {
return que.front();
}
// Return whether the stack is empty.bool empty() {
return que.empty();
}
};
Min Stack
/* Design a stack that supports push, pop, top,
* and retrieving the minimum element in constant time.
* push(x)-Push element x onto stack.
* pop()-Removes the element on top of the stack.
* top()-Get the top element.
* getMin()-Retrieve the minimum element in the stack.*/class MinStack {
public:
void push(int x) {
data.push(x);
if(min_data.size()==0 || x<min_data.top())
min_data.push(x);
else
min_data.push(min_data.top());
}
void pop() {
assert(data.size()>0 && min_data.size()>0);
data.pop();
min_data.pop();
}
int top() {
return data.top();
}
int getMin() {
assert(data.size()>0 && min_data.size()>0);
return min_data.top();
}
private:
stack<int> data;
stack<int> min_data;
};
树的迭代算法遍历
Trapping Water
int trap(vector<int>& height) {
int n=height.size();
int i, res = 0;
if(!n) return res;
vector<int> ltr(n, 0), rtl(n, 0);
ltr[0] = height[0];
rtl[n-1] = height[n-1];
for(i = 1; i < n; i++) ltr[i]=max(ltr[i-1], height[i]);
for(i = n-2; i >= 0; i--)rtl[i]=max(rtl[i+1], height[i]);
for(i = 0; i < n; i++) res += min(ltr[i], rtl[i])-height[i];
return res;
}
Largest Rectangle in Histogram
int largestRectangleArea(vector<int>& heights) {
height.push_back(0);
int n = height.size(), area = 0;
stack<int> indexes;
for (int i = 0; i < n; i++) {
while (!indexes.empty() && height[indexes.top()] > height[i]) {
int h = height[indexes.top()]; indexes.pop();
int l = indexes.empty() ? -1 : indexes.top();
area = max(area, h * (i - l - 1));
}
indexes.push(i);
}
return area;
}
class Queue {
public:
// Push element x to the back of queue.void push(int x) {
input.push(x);
}
// Removes the element from in front of queue.void pop(void) {
peek();
output.pop();
}
// Get the front element.int peek(void) {
if (output.empty())
while (input.size())
output.push(input.top()), input.pop();
return output.top();
}
// Return whether the queue is empty.bool empty(void) {
return input.empty() && output.empty();
}
private:
stack<int> input, output;
};