码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 算法训练第五十九天


    503. 下一个更大元素 II - 力扣(LeetCode)

    代码:

    1. class Solution {
    2. public:
    3. vector<int> nextGreaterElements(vector<int>& nums) {
    4. vector<int> nums1(nums.begin(), nums.end());
    5. nums.insert(nums.end(), nums1.begin(), nums1.end());
    6. // 用新的nums大小来初始化result
    7. vector<int> result(nums.size(), -1);
    8. if (nums.size() == 0) return result;
    9. // 开始单调栈
    10. stack<int> st;
    11. st.push(0);
    12. for (int i = 1; i < nums.size(); i++) {
    13. if (nums[i] < nums[st.top()]) st.push(i);
    14. else if (nums[i] == nums[st.top()]) st.push(i);
    15. else {
    16. while (!st.empty() && nums[i] > nums[st.top()]) {
    17. result[st.top()] = nums[i];
    18. st.pop();
    19. }
    20. st.push(i);
    21. }
    22. }
    23. // 最后再把结果集即result数组resize到原数组大小
    24. result.resize(nums.size() / 2);
    25. return result;
    26. }
    27. };

    42. 接雨水 - 力扣(LeetCode)

    代码:

    1. class Solution {
    2. public:
    3. int trap(vector<int>& height) {
    4. stack<int> st;
    5. st.push(0);
    6. int area = 0;
    7. for(int i = 1;i < height.size();i++)
    8. {
    9. if(height[i] <= height[st.top()])
    10. st.push(i);
    11. else
    12. {
    13. while(!st.empty() && height[i] > height[st.top()])
    14. {
    15. int mid = st.top();
    16. st.pop();
    17. if(!st.empty())
    18. {
    19. int h = min(height[i],height[st.top()]) - height[mid];
    20. int w = i - st.top() - 1;
    21. area += h * w;
    22. }
    23. }
    24. st.push(i);
    25. }
    26. }
    27. return area;
    28. }
    29. };

    84. 柱状图中最大的矩形 - 力扣(LeetCode)

    代码:

    1. class Solution {
    2. public:
    3. int largestRectangleArea(vector<int>& heights) {
    4. heights.insert(heights.begin(),0);
    5. heights.push_back(0);
    6. stack<int> st;
    7. st.push(0);
    8. int area = 0;
    9. for(int i = 1;i < heights.size();i++)
    10. {
    11. if(heights[i] >= heights[st.top()])
    12. st.push(i);
    13. else
    14. {
    15. while(!st.empty() && heights[i] < heights[st.top()])
    16. {
    17. int mid = st.top();
    18. st.pop();
    19. if(!st.empty())
    20. {
    21. int h = heights[mid];
    22. int w = i - st.top() - 1;
    23. area = max(area,h * w);
    24. }
    25. }
    26. st.push(i);
    27. }
    28. }
    29. return area;
    30. }
    31. };

  • 相关阅读:
    Day19 | 每天五道题
    掌握Flink键控状态(Keyed State):深入指南与实践
    为了工作刷题
    决策树(上):数据挖掘十大算法之一
    Python 学习 Day34
    操作系统------内存管理(2)非连续分配内存 段页式管理
    【Java网络编程】二
    重磅博文:可以找我咨询问题了
    深度学习——嵌入矩阵and学习词嵌入andWord2Vec
    企业电子招标采购系统源码Spring Boot + Mybatis + Redis + Layui + 前后端分离 构建企业电子招采平台之立项流程图
  • 原文地址:https://blog.csdn.net/zhangke_EX/article/details/132838792
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号