預備知識:ios
#include "pch.h" #include <iostream> #include <stdio.h> #include <stack> int main() { std::stack<int> S; if (S.empty()) { printf("S is empty"); } S.push(5); S.push(6); S.push(10); printf("S.top = %d\n", S.top()); S.pop(); S.pop(); printf("S.top = %d\n", S.top()); printf("S.size = %d\n", S.size()); return 0; } //S.top() 取出棧頂 //S.empty() 判斷棧是否爲空 //S.push(x) 把x壓入棧 //S.pop() 彈出棧頂 //S.size() 棧的存儲元素個數
#include "pch.h" #include <iostream> #include <stdio.h> #include <queue> int main() { std::queue<int> Q; if (Q.empty()) { printf("Q is empty!\n"); } Q.push(5); Q.push(6); Q.push(10); printf("Q.front = %d\n", Q.front()); Q.pop(); Q.pop(); printf("Q.front = %d\n", Q.front()); Q.push(1); printf("Q.back = %d\n", Q.back()); printf("Q.size = %d\n", Q.size()); return 0; } //Q.front() 取出隊列頭部元素 //Q.back() 取出隊列尾部元素 //Q.empty() 判斷隊列是否爲空 //Q.push(x) 把x添加至隊列 //Q.pop() 彈出隊列頭部元素 //Q.size() 返回隊列的存儲元素個數
要求棧用隊列實現,即在隊列裏面實現「後進先出」,因此把新進來的元素放入臨時隊列(temp_queue)中,再把原來的隊列中的元素添加至臨時隊列中,造成了最後新進來的元素在最前面。數組
代碼實現以下:less
class MyStack { public: /** Initialize your data structure here. */ MyStack() { } /** Push element x onto stack. */ void push(int x) { std::queue<int> tem_queue; tem_queue.push(x); while(!_data.empty()) { tem_queue.push(_data.front()); _data.pop(); } while(!tem_queue.empty()) { _data.push(tem_queue.front()); tem_queue.pop(); } } /** Removes the element on top of the stack and returns that element. */ int pop() { int x =_data.front(); _data.pop(); return x; } /** Get the top element. */ int top() { return _data.front(); }
將原數據壓入臨時棧,再講新數據壓入臨時棧,以後將臨時棧數據壓入原棧。函數
class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x to the back of queue. */ void push(int x) { std::stack<int>tem_stack; while(!_data.empty()) { tem_stack.push(_data.top()); _data.pop(); } tem_stack.push(x); while(!tem_stack.empty()) { _data.push(tem_stack.top()); tem_stack.pop(); } } /** Removes the element from in front of queue and returns that element. */ int pop() { int x = _data.top(); _data.pop(); return x; } /** Get the front element. */ int peek() { return _data.top(); } /** Returns whether the queue is empty. */ bool empty() { return _data.empty(); } private: std::stack<int>_data; };
class MinStack { public: /** initialize your data structure here. */ MinStack() { } void push(int x) { _data.push(x); if (_min.empty()) { _min.push(x); } else { if(x > _min.top()) x = _min.top(); _min.push(x); } } void pop() { _data.pop(); _min.pop(); } int top() { return _data.top(); } int getMin() { return _min.top(); } private: std::stack<int>_data; std::stack<int>_min; };
預備知識:spa
#include "pch.h" #include <iostream> #include <stdio.h> #include <queue> int main() { std::priority_queue<int>big_heap; std::priority_queue<int, std::vector<int>, std::greater<int>>small_heap; std::priority_queue<int, std::vector<int>, std::less<int>>big_heap2; if (big_heap.empty()) { printf("big_heap is empty!\n"); } int test[] = { 6, 10, 1, 7, 99, 4,33 }; for (int i = 0; i < 7; i++) { big_heap.push(test[i]); } printf("big_heap.top = %d\n", big_heap.top()); big_heap.push(1000); printf("big_heap.top = %d\n", big_heap.top()); for (int i = 0; i < 3; i++) { big_heap.pop(); } printf("big_heap.top= %d\n", big_heap.top()); printf("big_heap.size = %d\n", big_heap.size()); }
class Solution { public: int findKthLargest(vector<int>& nums, int k) { std::priority_queue<int, std::vector<int>, std::greater<int>> Q; for(int i = 0; i< nums.size(); i++) { if(i < k) #這裏我以爲是i < k { Q.push(nums[i]); } else if(nums[i] > Q.top()) { Q.pop(); Q.push(nums[i]); } } return Q.top(); } };
第一種狀況:code
第二種狀況:blog
第三種狀況:隊列
class MedianFinder { public: /** initialize your data structure here. */ std::priority_queue<int, std::vector<int>, std::greater<int>>small_queue; std::priority_queue<int, std::vector<int>, std::less<int>>big_queue; 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()){ small_queue.push(num); } else{ small_queue.push(big_queue.top()); big_queue.pop(); big_queue.push(num); } } else if(small_queue.size() > big_queue.size() ) { if(num < small_queue.top()){ big_queue.push(num); } else{ big_queue.push(small_queue.top()); small_queue.pop(); small_queue.push(num); } } } double findMedian() { if(big_queue.size() == small_queue.size()){ return (float((big_queue.top()) + small_queue.top()) / 2); //注意須要進行浮點數轉換 } else if(big_queue.size() > small_queue.size()){ return big_queue.top(); } return small_queue.top(); } };