二、算法分类经典题目练习:栈、队列、堆

225.用队列实现栈
临时队列

class MyStack {
     
public:
    /** Initialize your data structure here. */
    MyStack() {
     

    }
    
    /** Push element x onto stack. */
    void push(int x) {
     
        queue<int> temp_queue;
        temp_queue.push(x);
        while(!q.empty()){
     
            temp_queue.push(q.front());
            q.pop();
        }
        while(!temp_queue.empty()){
     
            q.push(temp_queue.front());
            temp_queue.pop();
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
     
        int res = q.front();
        q.pop();
        return res;
    }
    
    /** Get the top element. */
    int top() {
     
        return q.front();
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
     
        return q.empty();
    }
private:
    queue<int> q;
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack* obj = new MyStack();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->top();
 * bool param_4 = obj->empty();
 */

232.用栈实现队列
临时栈

class MyQueue {
     
public:
    /** Initialize your data structure here. */
    MyQueue() {
     

    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
     
        stack<int> temp_stack;
        while(!s.empty()){
     
            temp_stack.push(s.top());
            s.pop();
        }
        s.push(x);
        while(!temp_stack.empty()){
     
            s.push(temp_stack.top());
            temp_stack.pop();
        }
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
     
        int res = s.top();
        s.pop();
        return res;
    }
    
    /** Get the front element. */
    int peek() {
     
        return s.top();
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
     
        return s.empty();
    }
private:
    stack<int> s;
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue* obj = new MyQueue();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->peek();
 * bool param_4 = obj->empty();
 */

155.最小栈

class MinStack {
     
public:
    /** initialize your data structure here. */
    MinStack() {
     
        
    }
    
    void push(int x) {
     
        s.push(x);
        if(!min.empty()){
     
            x = min.top() < x ? min.top() : x;
            min.push(x);
        }else{
     
            min.push(x);
        }
    }
    
    void pop() {
     
        s.pop();
        min.pop();
    }
    
    int top() {
     
        return s.top();
    }
    
    int getMin() {
     
        return min.top();
    }

private:
    stack<int> s;
    stack<int> min;
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj->push(x);
 * obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->getMin();
 */

合法的出栈顺序
http://poj.org/problem?id=1363

#include 

#include 
#include 

bool check_is_valid_order(std::queue<int> &order){
     
	std::stack<int> S;
	int n = order.size();
	for (int i = 1; i <= n; i++){
     
		S.push(i);
		while(!S.empty() && order.front() == S.top()){
     
			S.pop();
			order.pop();
		}
	}
	if (!S.empty()){
     
		return false;
	}
	return true;
}

int main(){
     
	int n;
	int train;
	scanf("%d", &n);
	while(n){
     
		scanf("%d", &train);
		while (train){
     
			std::queue<int> order;
			order.push(train);
			for (int i = 1; i < n; i++){
     
				scanf("%d", &train);
				order.push(train);
			}
			if (check_is_valid_order(order)){
     
				printf("Yes\n");
			}
			else{
     
				printf("No\n");
			}
			scanf("%d", &train);
		}
		printf("\n");
		scanf("%d", &n);
	}
	return 0;
}

224.基本计算器

class Solution {
     
public:
    int calculate(string s) {
     
        stack<int> st;
        int n = s.size(), res = 0, sign = 1;
        for(int i = 0; i < n; ++i){
     
            int num = 0;
            if(s[i]>='0'){
     
                while(i<n&&s[i]>='0'){
     
                    num = num * 10 + (s[i] - '0');
                    i++;
                }
                i--;
                res += num * 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]==')'){
     
                res *= st.top(); st.pop();
                res += st.top(); st.pop();
            }
        }
        return res;
    }
};

215.数组中的第K个最大的元素
优先队列

class Solution {
     
public:
    int findKthLargest(vector<int>& nums, int k) {
     
        priority_queue<int, vector<int>, greater<int>> q;
        for(int i = 0; i < nums.size(); ++i){
     
            if(q.size() < k){
     
                q.push(nums[i]);
            }else if(q.top() < nums[i]){
     
                q.pop();
                q.push(nums[i]);
            }
        }
        return q.top();
    }
};

295.数据流的中位数
大顶堆小顶堆

class MedianFinder {
     
public:
    /** initialize your data structure here. */
    MedianFinder() {
     

    }
    
    void addNum(int num) {
     
        if(big_queue.empty()){
     
            big_queue.push(num);
            return;
        }
        if(big_queue.size()==small_queue.size()){
     
            if(num < big_queue.top()){
     
                big_queue.push(num);
            }else{
     
                small_queue.push(num);
            }
        }else if(big_queue.size() > small_queue.size()){
     
            if(num < big_queue.top()){
     
                int tmp = big_queue.top();
                big_queue.pop();
                small_queue.push(tmp);
                big_queue.push(num);
            }else{
     
                small_queue.push(num);
            }
        }else{
     
            if(num > small_queue.top()){
     
                int tmp = small_queue.top();
                small_queue.pop();
                small_queue.push(num);
                big_queue.push(tmp);
            }else{
     
                big_queue.push(num);
            }
        }
    }
    
    double findMedian() {
     
        if(big_queue.size()==small_queue.size()){
     
            return (double)(big_queue.top()+small_queue.top())/2;
        }else if(big_queue.size()>small_queue.size()){
     
            return big_queue.top();
        }else{
     
            return small_queue.top();
        }
    }
private:
    priority_queue<int, vector<int>, greater<int>> small_queue;
    priority_queue<int, vector<int>, less<int>> big_queue;
};

/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder* obj = new MedianFinder();
 * obj->addNum(num);
 * double param_2 = obj->findMedian();
 */

你可能感兴趣的:(算法分类练习,算法,队列,堆栈)