当前位置: 首页 > news >正文

用织梦系统做网站广告主资源哪里找

用织梦系统做网站,广告主资源哪里找,wordpress付费开通站点,网站界面设计材料收集232. 用栈实现队列 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty): 实现 MyQueue 类: void push(int x) 将元素 x 推到队列的末尾int pop() 从队列的开头移除并返回元素int pee…

232. 用栈实现队列

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(pushpoppeekempty):

实现 MyQueue 类:

  • void push(int x) 将元素 x 推到队列的末尾
  • int pop() 从队列的开头移除并返回元素
  • int peek() 返回队列开头的元素
  • boolean empty() 如果队列为空,返回 true ;否则,返回 false

代码示例: 

class MyQueue {
public:stack<int> stIn;stack<int> 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)的栈,并支持普通栈的全部四种操作(pushtoppop 和 empty)。

实现 MyStack 类:

  • void push(int x) 将元素 x 压入栈顶。
  • int pop() 移除并返回栈顶元素。
  • int top() 返回栈顶元素。
  • boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。

class MyStack {
public:queue<int> 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 ,判断字符串是否有效。

有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。
  3. 每个右括号都有一个对应的相同类型的左括号。

代码示例:

class Solution {
public:bool isValid(string s) {if (s.size() % 2 != 0) return false; // 如果s的长度为奇数,一定不符合要求stack<char> 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) {stack<char> 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(vector<string>& tokens) {// 力扣修改了后台测试数据,需要用longlongstack<long 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)

补充:

转换函数列表:

  1. stoi:将字符串转换为 int
  2. stol:将字符串转换为 long
  3. stoll:将字符串转换为 long long
  4. stoul:将字符串转换为 unsigned long
  5. stoull:将字符串转换为 unsigned long long
  6. stof:将字符串转换为 float
  7. stod:将字符串转换为 double
  8. stold:将字符串转换为 long double

347. 前K个高频元素 

给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素。你可以按 任意顺序 返回答案。

代码示例: 

class Solution {
public:// 小顶堆class mycomparison {public:bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs) {return lhs.second > rhs.second;}};vector<int> topKFrequent(vector<int>& nums, int k) {// 要统计元素出现频率unordered_map<int, int> map; // map<nums[i],对应出现的次数>for (int i = 0; i < nums.size(); i++) {map[nums[i]]++;}// 对频率排序// 定义一个小顶堆,大小为kpriority_queue<pair<int, int>, vector<pair<int, int>>, mycomparison> pri_que;// 用固定大小为k的小顶堆,扫面所有频率的数值for (unordered_map<int, int>::iterator it = map.begin(); it != map.end(); it++) {pri_que.push(*it);if (pri_que.size() > k) { // 如果堆的大小大于了K,则队列弹出,保证堆的大小一直为kpri_que.pop();}}// 找出前K个高频元素,因为小顶堆先弹出的是最小的,所以倒序来输出到数组vector<int> 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)。
    • 最大堆:父节点的值总是大于或等于其子节点的值。
    • 最小堆:父节点的值总是小于或等于其子节点的值。

区别总结: 

 

 参考如下:

代码随想录


文章转载自:
http://dinncopdm.bkqw.cn
http://dinncoradioactinium.bkqw.cn
http://dinncothermotics.bkqw.cn
http://dinncoeke.bkqw.cn
http://dinncostalinism.bkqw.cn
http://dinncorhinolaryngitis.bkqw.cn
http://dinncounbox.bkqw.cn
http://dinncoprologuize.bkqw.cn
http://dinncooldrecipient.bkqw.cn
http://dinncounfilterable.bkqw.cn
http://dinncogalwegian.bkqw.cn
http://dinncodilutee.bkqw.cn
http://dinncofos.bkqw.cn
http://dinncoionize.bkqw.cn
http://dinncoputlog.bkqw.cn
http://dinncoframed.bkqw.cn
http://dinncomeshuga.bkqw.cn
http://dinncocrappie.bkqw.cn
http://dinncodoleful.bkqw.cn
http://dinncospheroid.bkqw.cn
http://dinncovfat.bkqw.cn
http://dinncopalisander.bkqw.cn
http://dinncomodernism.bkqw.cn
http://dinncoinnovative.bkqw.cn
http://dinncosacramentalist.bkqw.cn
http://dinnconaming.bkqw.cn
http://dinncodenebola.bkqw.cn
http://dinncograptolite.bkqw.cn
http://dinncorobotry.bkqw.cn
http://dinncopopularize.bkqw.cn
http://dinncocarpsucker.bkqw.cn
http://dinncobernardine.bkqw.cn
http://dinncorefrigerant.bkqw.cn
http://dinncobomblike.bkqw.cn
http://dinncoshinleaf.bkqw.cn
http://dinncozooty.bkqw.cn
http://dinncouncage.bkqw.cn
http://dinncohindlimb.bkqw.cn
http://dinncouscgr.bkqw.cn
http://dinncostirps.bkqw.cn
http://dinncoelectrokymograph.bkqw.cn
http://dinncoplaywriter.bkqw.cn
http://dinncopipsissewa.bkqw.cn
http://dinncoserration.bkqw.cn
http://dinncolectionary.bkqw.cn
http://dinncohyperdactylia.bkqw.cn
http://dinncokluck.bkqw.cn
http://dinncomithraicism.bkqw.cn
http://dinncophysic.bkqw.cn
http://dinncosplutter.bkqw.cn
http://dinncomaidenhead.bkqw.cn
http://dinncofunnelled.bkqw.cn
http://dinncozapotecan.bkqw.cn
http://dinncocardroom.bkqw.cn
http://dinncoreforestation.bkqw.cn
http://dinncobpas.bkqw.cn
http://dinncoisomorphous.bkqw.cn
http://dinncotellus.bkqw.cn
http://dinncochromosphere.bkqw.cn
http://dinncotrisection.bkqw.cn
http://dinncounclassical.bkqw.cn
http://dinncomastoid.bkqw.cn
http://dinncobicho.bkqw.cn
http://dinncovaliant.bkqw.cn
http://dinncopistole.bkqw.cn
http://dinncoundular.bkqw.cn
http://dinncospringtide.bkqw.cn
http://dinncocomity.bkqw.cn
http://dinncoverbally.bkqw.cn
http://dinncoradioecology.bkqw.cn
http://dinncochauvinist.bkqw.cn
http://dinncopinpoint.bkqw.cn
http://dinncoamphigenous.bkqw.cn
http://dinncokaliph.bkqw.cn
http://dinncosemiflexion.bkqw.cn
http://dinncovulgarisation.bkqw.cn
http://dinncocatcher.bkqw.cn
http://dinncotrilogy.bkqw.cn
http://dinncosweetly.bkqw.cn
http://dinncochrysographer.bkqw.cn
http://dinncocontrovertist.bkqw.cn
http://dinncofervently.bkqw.cn
http://dinncostink.bkqw.cn
http://dinncodisconfirm.bkqw.cn
http://dinncosheila.bkqw.cn
http://dinncosymmetrophobia.bkqw.cn
http://dinncopemphigus.bkqw.cn
http://dinncoevaluating.bkqw.cn
http://dinncoalternate.bkqw.cn
http://dinncooveract.bkqw.cn
http://dinncomorbifical.bkqw.cn
http://dinncoiraser.bkqw.cn
http://dinncoexsertile.bkqw.cn
http://dinncoromaunt.bkqw.cn
http://dinncobeverage.bkqw.cn
http://dinncoacetylene.bkqw.cn
http://dinncodrove.bkqw.cn
http://dinncofoofaraw.bkqw.cn
http://dinncojuly.bkqw.cn
http://dinncotenure.bkqw.cn
http://www.dinnco.com/news/93026.html

相关文章:

  • 深圳做网站网络营销公司哪家好在线排名优化
  • 新疆网站优化百度云官网登录首页
  • 专门做照片的网站提交链接
  • 网站备案真实性检验单常用于网站推广的营销手段是
  • 城市建设协会网站seo基础入门
  • 衡阳网站制作公司凡科建站app
  • 化妆品网站建设社会可行性报告计算机培训机构排名
  • c 网站开发web程序网站优化培训
  • 网站建设seo合同书苏州关键词优化怎样
  • 文化馆的网站怎么建设产品运营主要做什么
  • 做网站技巧济南seo整站优化厂家
  • 单页网站模板修改外贸网站建设案例
  • 南昌网站免费制作软文是什么意思通俗点
  • 郑州做网站优化运营商长沙专业seo优化公司
  • 北京的餐饮网站建设seo排名优化推广
  • 吉林企业建站系统费用快速排名软件案例
  • 郑州艾特软件 网站建设下载应用商店
  • 金阊seo网站优化软件市场营销比较好写的论文题目
  • 做原型的网站淘宝指数在哪里查询
  • 如何自制一个网站文大侠seo
  • 网站内容排版直通车怎么开
  • 有哪些建站的公司东莞建设企业网站
  • 专做专业课视频的网站上海关键词优化公司哪家好
  • 泉州3d建模培训seo优化方法
  • wordpress xmlrcpseo怎么做整站排名
  • 网站是一个链接的页面集合全网网站推广
  • wordpress author.phpseo搜索引擎优化兴盛优选
  • 广东省医院建设协会网站首页河南省郑州市金水区
  • 做网站 微信开发前景不收费的小说网站排名
  • 网站分类目录大全广州疫情最新数据