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

中国建设银行官网站企业百度云网页版入口

中国建设银行官网站企业,百度云网页版入口,网站群建设规范,b站推广视频​ LeetCode 503 下一个更大元素II 题目链接:https://leetcode.cn/problems/next-greater-element-ii/ 思路: 方法一:两个for循环遍历单调栈 第一个for循环确定数组中的某个值在右边有最大的数,第二个for循环是为了可以使数组变成循环数…

LeetCode 503 下一个更大元素II

题目链接:https://leetcode.cn/problems/next-greater-element-ii/

思路:

方法一:两个for循环遍历单调栈

第一个for循环确定数组中的某个值在右边有最大的数,第二个for循环是为了可以使数组变成循环数组
例子:[5,4,3,2,1]
1、栈里 4,3,2,1,0](右边为栈顶,栈内元素为下标)
2、从下标0开始再次循环
(模拟一次就目标了)

代码:

class Solution {
public:vector<int> nextGreaterElements(vector<int>& nums) {vector<int>result(nums.size(), -1);stack<int>st;st.push(0);for(int i = 1; i < nums.size(); i++){if(nums[i] <= nums[st.top()])st.push(i);else{while(!st.empty() && nums[i] > nums[st.top()]){result[st.top()] = nums[i];st.pop();}st.push(i);}}for(int i = 0; i < nums.size(); i++){if(nums[i] <= nums[st.top()])st.push(i);else{while(!st.empty() && nums[i] > nums[st.top()]){result[st.top()] = nums[i];st.pop();}st.push(i);}}return result;}
};

方法二:单调栈,用取模的方法对数组进行循环

代码

class Solution {
public:vector<int> nextGreaterElements(vector<int>& nums) {vector<int>result(nums.size(), -1);stack<int>st;st.push(0);for(int i = 1; i < nums.size() * 2; i++){if(nums[i % nums.size()] <= nums[st.top()])st.push(i % nums.size());else{while(!st.empty() && nums[i % nums.size()] > nums[st.top()]){result[st.top()] = nums[i % nums.size()];st.pop();}st.push(i % nums.size());}}return result;}
};

总结

关键在于如何循环数组


LeetCode 42 接雨水

题目链接:https://leetcode.cn/problems/trapping-rain-water/

思路:

本题关键点:

  1. 接雨水重点在于要找当前元素左边第一个比它的元素和右边第一个比它大的元素
  2. 接雨水是按行来计算的
    在这里插入图片描述
  3. 明确h和w是如何计算的,w在计算中必须还要减1

代码

class Solution {
public:int trap(vector<int>& height) {int result = 0;stack<int>st;st.push(0);for(int i = 1; i < height.size(); i++){if(height[i] <= height[st.top()])st.push(i);else{while(!st.empty() && height[i] > height[st.top()]){int mid = st.top();st.pop();if(!st.empty()){int h = min(height[i], height[st.top()]) - height[mid];int w = i - st.top() - 1;result += h * w;}}st.push(i);}}return result;}
};

总结

接雨水问题是经典问题,后续要多加练习


今日总结:

还有一天,加油!

http://www.ritt.cn/news/28698.html

相关文章:

  • 余姚做轴承网站网络平台建设及运营方案
  • 做博客网站怎么赚钱seo教程技术资源
  • 手机网站在线生成网络推广平台
  • 海外网络推广专员seo优化视频教程
  • 宁波网站推广排名seo简介
  • 个人导航网站怎么备案seminar是什么意思
  • 信宜市建设局网站2021百度新算法优化
  • 做网站资料准备百度seo排名软
  • 陈铭生怎么死的网站推广seo教程
  • 域名买卖网站广州百度网站快速排名
  • 宁波模板建站哪家服务专业2023网站推广入口
  • 广州淘宝网站建设b2b平台网站
  • 失业保险网站网页代码模板
  • 做网站个人品牌营销推广
  • 做网站江西免费大数据网站
  • 做淘宝客找商品网站有哪些已矣seo排名点击软件
  • 巨野县建设局网站优化大师好用吗
  • 网站在线支付方案深圳在线制作网站
  • 建设门户网站的请示中小企业网站制作
  • 室内设计网站集合上海谷歌seo推广公司
  • 淮南商城网站建设地址深圳龙岗区优化防控措施
  • 张家口做网站公司2023年新闻摘抄十条
  • 做网站搞活动百度sem竞价托管公司
  • 进入网站后台如何操作好看的网站设计
  • 网站如何做线上和线下推广上海seo
  • 网站 ip修改备案流程seo体系百科
  • 动态网站开发作业网站优化外包找谁
  • 揭阳专业网站建设广州网站优化系统
  • 高爆传奇手机版安卓优化大师hd
  • 东莞大岭山镇网站建设apple私人免费网站怎么下载