哪些网站可以做爬虫实验,网站网站建设网站,电动工具咋在网上做网站,女性门户网站模板232. 用栈实现队列 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作#xff08;push、pop、peek、empty#xff09;#xff1a; 实现 MyQueue 类#xff1a; void push(int x) 将元素 x 推到队列的末尾int pop() 从队列的开头移除并返回元素int pee…232. 用栈实现队列 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作push、pop、peek、empty 实现 MyQueue 类 void push(int x) 将元素 x 推到队列的末尾int pop() 从队列的开头移除并返回元素int peek() 返回队列开头的元素boolean empty() 如果队列为空返回 true 否则返回 false 代码示例
class MyQueue {
public:stackint stIn;stackint stOut;/** Initialize your data structure here. */MyQueue() {}/** Push element x to the back of queue. */void push(int x) {stIn.push(x);}/** Removes the element from in front of queue and returns that element. */int pop() {// 只有当stOut为空的时候再从stIn里导入数据导入stIn全部数据if (stOut.empty()) {// 从stIn导入数据直到stIn为空while(!stIn.empty()) {stOut.push(stIn.top());stIn.pop();}}int result stOut.top();stOut.pop();return result;}/** Get the front element. */int peek() {int res this-pop(); // 直接使用已有的pop函数stOut.push(res); // 因为pop函数弹出了元素res所以再添加回去return res;}/** Returns whether the queue is empty. */bool empty() {return stIn.empty() stOut.empty();}
}; 复杂度分析 时间复杂度push和empty为O(1), pop和peek为O(n)空间复杂度O(n) 225. 用队列实现栈 请你仅使用两个队列实现一个后入先出LIFO的栈并支持普通栈的全部四种操作push、top、pop 和 empty。 实现 MyStack 类 void push(int x) 将元素 x 压入栈顶。int pop() 移除并返回栈顶元素。int top() 返回栈顶元素。boolean empty() 如果栈是空的返回 true 否则返回 false 。 class MyStack {
public:queueint que;/** Initialize your data structure here. */MyStack() {}/** Push element x onto stack. */void push(int x) {que.push(x);}/** Removes the element on top of the stack and returns that element. */int pop() {int size que.size();size--;while (size--) { // 将队列头部的元素除了最后一个元素外 重新添加到队列尾部que.push(que.front());que.pop();}int result que.front(); // 此时弹出的元素顺序就是栈的顺序了que.pop();return result;}/** Get the top element. */int top() {return que.back();}/** Returns whether the stack is empty. */bool empty() {return que.empty();}
}; 复杂度分析 时间复杂度pop为O(n)其他为O(1)空间复杂度O(n) 20. 有效的括号 给定一个只包括 (){}[] 的字符串 s 判断字符串是否有效。 有效字符串需满足 左括号必须用相同类型的右括号闭合。左括号必须以正确的顺序闭合。每个右括号都有一个对应的相同类型的左括号。 代码示例
class Solution {
public:bool isValid(string s) {if (s.size() % 2 ! 0) return false; // 如果s的长度为奇数一定不符合要求stackchar st;for (int i 0; i s.size(); i) {if (s[i] () st.push());else if (s[i] {) st.push(});else if (s[i] [) st.push(]);// 第三种情况遍历字符串匹配的过程中栈已经为空了没有匹配的字符了说明右括号没有找到对应的左括号 return false// 第二种情况遍历字符串匹配的过程中发现栈里没有我们要匹配的字符。所以return falseelse if (st.empty() || st.top() ! s[i]) return false;else st.pop(); // st.top() 与 s[i]相等栈弹出元素}// 第一种情况此时我们已经遍历完了字符串但是栈不为空说明有相应的左括号没有右括号来匹配所以return false否则就return truereturn st.empty();}
};复杂度分析 时间复杂度O(n)空间复杂度O(n) 1047. 删除字符串中所有相邻重复项 给出由小写字母组成的字符串 S重复项删除操作会选择两个相邻且相同的字母并删除它们。 在 S 上反复执行重复项删除操作直到无法继续删除。 在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。 代码示例
法一使用栈来存放
class Solution {
public:string removeDuplicates(string S) {stackchar st;for (char s : S) {if (st.empty() || s ! st.top()) {st.push(s);} else {st.pop(); // s 与 st.top()相等的情况}}string result ;while (!st.empty()) { // 将栈中元素放到result字符串汇总result st.top();st.pop();}reverse (result.begin(), result.end()); // 此时字符串需要反转一下return result;}
};
法二拿字符串直接作为栈
class Solution {
public:string removeDuplicates(string S) {string result;for(char s : S) {if(result.empty() || result.back() ! s) {result.push_back(s);}else {result.pop_back();}}return result;}
}; 复杂度分析 时间复杂度O(n)空间复杂度O(n) 150. 逆波兰表达式 给你一个字符串数组 tokens 表示一个根据 逆波兰表示法 表示的算术表达式。 请你计算该表达式。返回一个表示表达式值的整数。 注意 有效的算符为 、-、* 和 / 。每个操作数运算对象都可以是一个整数或者另一个表达式。两个整数之间的除法总是 向零截断 。表达式中不含除零运算。输入是一个根据逆波兰表示法表示的算术表达式。答案及所有中间计算结果可以用 32 位 整数表示。 代码示例
class Solution {
public:int evalRPN(vectorstring tokens) {// 力扣修改了后台测试数据需要用longlongstacklong long st; for (int i 0; i tokens.size(); i) {if (tokens[i] || tokens[i] - || tokens[i] * || tokens[i] /) {long long num1 st.top();st.pop();long long num2 st.top();st.pop();if (tokens[i] ) st.push(num2 num1);if (tokens[i] -) st.push(num2 - num1);if (tokens[i] *) st.push(num2 * num1);if (tokens[i] /) st.push(num2 / num1);} else {st.push(stoll(tokens[i]));//将其转换为 long long 类型并压入栈中}}int result st.top();st.pop(); // 把栈里最后一个元素弹出其实不弹出也没事return result;}
}; 复杂度分析 时间复杂度O(n)空间复杂度O(n) 补充 转换函数列表 stoi将字符串转换为 int。stol将字符串转换为 long。stoll将字符串转换为 long long。stoul将字符串转换为 unsigned long。stoull将字符串转换为 unsigned long long。stof将字符串转换为 float。stod将字符串转换为 double。stold将字符串转换为 long double。 347. 前K个高频元素 给你一个整数数组 nums 和一个整数 k 请你返回其中出现频率前 k 高的元素。你可以按 任意顺序 返回答案。 代码示例
class Solution {
public:// 小顶堆class mycomparison {public:bool operator()(const pairint, int lhs, const pairint, int rhs) {return lhs.second rhs.second;}};vectorint topKFrequent(vectorint nums, int k) {// 要统计元素出现频率unordered_mapint, int map; // mapnums[i],对应出现的次数for (int i 0; i nums.size(); i) {map[nums[i]];}// 对频率排序// 定义一个小顶堆大小为kpriority_queuepairint, int, vectorpairint, int, mycomparison pri_que;// 用固定大小为k的小顶堆扫面所有频率的数值for (unordered_mapint, int::iterator it map.begin(); it ! map.end(); it) {pri_que.push(*it);if (pri_que.size() k) { // 如果堆的大小大于了K则队列弹出保证堆的大小一直为kpri_que.pop();}}// 找出前K个高频元素因为小顶堆先弹出的是最小的所以倒序来输出到数组vectorint result(k);for (int i k - 1; i 0; i--) {result[i] pri_que.top().first;pri_que.pop();}return result;}
}; 复杂度分析 时间复杂度O(nlogk)空间复杂度O(n) 补充 堆特性 优先队列基于堆的数据结构通常分为最大堆max-heap和最小堆min-heap。 最大堆父节点的值总是大于或等于其子节点的值。最小堆父节点的值总是小于或等于其子节点的值。 区别总结 参考如下 代码随想录