日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构和算法——栈、队列、堆

發布時間:2024/7/19 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构和算法——栈、队列、堆 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

        • 1.預備知識
          • 1.1 棧
          • 1.2 隊列
          • 1.3 堆
        • 2.用隊列實現棧
          • 2.1 題目描述
          • 2.2 解題思路
          • 2.3 C++實現
        • 3.用棧實現隊列
          • 3.1 題目描述
          • 3.2 解題思路
          • 3.3 C++實現
        • 4.最小棧
          • 4.1 題目描述
          • 4.2 解題思路
        • 5.合法的出棧序列
          • 5.1 題目描述
          • 5.2 解題思路
          • 5.3 C++實現
        • 6.基本計算器
          • 6.1 題目描述
          • 6.2 解題思路
        • 7.數組中的第K個最大元素
          • 7.1 題目描述
          • 7.2 解題思路
          • 7.3 C++實現
        • 8.數據流的中位數
          • 8.1 題目描述
          • 8.2 解題思路
          • 8.3 C++實現

1.預備知識

1.1 棧

S.empty() //1 S.push(10); //2 S.pop(); //3
1.2 隊列

Q.push(5); //1 Q.pop(); //2 Q.push(1); //3
1.3 堆

圖:堆
圖:堆的函數

big_heap.push(1000); //1 i<3 // 2 big_heap.pop(); //3

2.用隊列實現棧

2.1 題目描述

請你僅使用兩個隊列實現一個后入先出(LIFO)的棧,并支持普通隊列的全部四種操作(push、top、pop 和 empty)

實現 MyStack 類:
void push(int x) 將元素 x 壓入棧頂。
int pop() 移除并返回棧頂元素。
int top() 返回棧頂元素。
boolean empty() 如果棧是空的,返回 true ;否則,返回 false 。

你只能使用隊列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 這些操作。

你所使用的語言也許不支持隊列。 你可以使用 list (列表)或者 deque(雙端隊列)來模擬一個隊列 , 只要是標準的隊列操作即可。

2.2 解題思路

2.3 C++實現
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(!_data.empty()){temp_queue.push(_data.front());_data.pop();}while(!temp_queue.empty()){_data.push(temp_queue.front());temp_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();}/** Returns whether the stack is empty. */bool empty() {return _data.empty();} private:queue<int> _data; };

3.用棧實現隊列

3.1 題目描述

請你僅使用兩個棧實現先入先出隊列。隊列應當支持一般隊列支持的所有操作(push、pop、peek、empty):

實現 MyQueue 類:
void push(int x) 將元素 x 推到隊列的末尾
int pop() 從隊列的開頭移除并返回元素
int peek() 返回隊列開頭的元素
boolean empty() 如果隊列為空,返回 true ;否則,返回 false

說明:
你只能使用標準的棧操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的語言也許不支持棧。你可以使用 list 或者 deque(雙端隊列)來模擬一個棧,只要是標準的棧操作即可。

3.2 解題思路

3.3 C++實現
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(!_data.empty()){temp_stack.push(_data.top());_data.pop();}temp_stack.push(x);while(!temp_stack.empty()){_data.push(temp_stack.top());temp_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:stack<int> _data; };

4.最小棧

4.1 題目描述

設計一個支持 push ,pop ,top 操作,并能在常數時間內檢索到最小元素的棧。

push(x) —— 將元素 x 推入棧中。 pop() —— 刪除棧頂的元素。 top() —— 獲取棧頂元素。 getMin() —— 檢索棧中的最小元素。
4.2 解題思路
class MinStack { public:/** initialize your data structure here. */MinStack() {}void push(int val) {_data.push(val);if(_min.empty()){_min.push(val);}else{if(val>_min.top()){val=_min.top();}_min.push(val);}}void pop() {_data.pop();_min.pop();}int top() {return _data.top();}int getMin() {return _min.top();} private:stack<int> _data;stack<int> _min; };

5.合法的出棧序列

5.1 題目描述

5.2 解題思路

圖1:模擬入棧出棧
圖2:步驟1

圖3:步驟2

圖4:步驟3
圖5:步驟4

5.3 C++實現
#include <iostream> #include<queue> #include<stack> using namespace std;bool check_is_valid_order(queue<int>& order) {stack<int> S;int n = order.size();for (int i = 1; i <= n; i++) {S.push(i);while (!S.empty() && S.top() == order.front()) {S.pop();order.pop();}}if (!S.empty()) {return false;}return true; }int main() {//合法:32541 ;非法:31245queue<int> order1, order2;bool a1, a2;order1.push(3);order1.push(2);order1.push(5);order1.push(4);order1.push(1);a1 = check_is_valid_order(order1);if (a1 == true) {cout << "32541合法" << endl;}else {cout << "32541非法" << endl;}order2.push(3);order2.push(1);order2.push(2);order2.push(4);order2.push(5);a2 = check_is_valid_order(order2);if (a2 == true) {cout << "31245合法" << endl;}else {cout << "31245非法" << endl;}return 0; }

6.基本計算器

6.1 題目描述

給你一個字符串表達式 s ,請你實現一個基本計算器來計算并返回它的值。

1 <= s.length <= 3 * 105 s 由數字、'+'、'-'、'('、')'、和 ' ' 組成 s 表示一個有效的表達式
6.2 解題思路

圖1:棧內元素計算

圖2:使用棧處理優先級

圖3:將字符串轉化為數字

圖4:計算函數

圖5:字符串處理思路

7.數組中的第K個最大元素

7.1 題目描述

在未排序的數組中找到第 k 個最大的元素。請注意,你需要找的是數組排序后的第 k 個最大的元素,而不是第 k 個不同的元素。

7.2 解題思路

圖1:思路
圖2:思路

7.3 C++實現
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();} };

8.數據流的中位數

8.1 題目描述

中位數是有序列表中間的數。如果列表長度是偶數,中位數則是中間兩個數的平均值。

例如,
[2,3,4] 的中位數是 3
[2,3] 的中位數是 (2 + 3) / 2 = 2.5

設計一個支持以下兩種操作的數據結構:
void addNum(int num) - 從數據流中添加一個整數到數據結構中。
double findMedian() - 返回目前所有元素的中位數。

8.2 解題思路


8.3 C++實現
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()){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(small_queue.size()==big_queue.size()){return (small_queue.top()+big_queue.top())/2;}else if(small_queue.size()>big_queue.size()){return small_queue.top();}return big_queue.top();} private:priority_queue<double,vector<double>,greater<double>> small_queue;priority_queue<double,vector<double>,less<double>> big_queue; };

總結

以上是生活随笔為你收集整理的数据结构和算法——栈、队列、堆的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。