数据结构-栈和队列

  • STL:stack
  • 操作:push、pop、top、size、empty、swap
  • 应用:
Valid Parentheses
    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()) return false;
                if ((s[i]==')') && (st.top()!='(')) return false;
                if ((s[i]=='}') && (st.top()!='{')) return false;
                if ((s[i]==']') && (st.top()!='[')) return false;
                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;
        }
        else if(s[i]=='+') sign=1;
        else if(s[i]=='-') sign=-1;
        else if(s[i]=='('){
            st.push(res);
            st.push(sign);
            res=0;
            sign=1;
        }
        else if(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; 
    }
Maximal Rectangle
Simplify Path
string simplifyPath(string path) {
        string result="", token;
        stringstream ss(path);
        vector<string> tokens;
        while(getline(ss, token, '/')){
            if(token=="." || token=="") continue;
            else if(token==".."){
                if(tokens.size()!=0)  tokens.pop_back();
            }
            else tokens.push_back(token);
        }
        if(tokens.size()==0) return "/";
        for(int i=0; i<tokens.size(); ++i)
            result=result+'/'+tokens[i];
        return result;
    }

队列

  • STL:deque、queue、priority_queue
  • Queue操作:push、pop、front、back、size、empty、swap
  • Priority_Queue操作:push、pop、front、size、empty、swap(最大堆:队首元素为队列中最大的元素)
  • 优先队列和堆结合
Implement Stack using Queues
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;
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值