https://leetcode.com/problems/next-greater-element-ii/description/

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

503. Next Greater Element II的更多相关文章

  1. [LeetCode] 503. Next Greater Element II 下一个较大的元素 II

    Given a circular array (the next element of the last element is the first element of the array), pri ...

  2. LeetCode - 503. Next Greater Element II

    Given a circular array (the next element of the last element is the first element of the array), pri ...

  3. 【LeetCode】503. Next Greater Element II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 单调递减栈 日期 题目地址:https:/ ...

  4. 496. Next Greater Element I + 503. Next Greater Element II + 556. Next Greater Element III

    ▶ 给定一个数组与它的一个子列,对于数组中的一个元素,定义它右边第一个比他大的元素称为他的后继,求所给子列的后继构成的数组 ▶ 第 496 题,规定数组最后一个元素即数组最大元素的后继均为 -1 ● ...

  5. 503 Next Greater Element II 下一个更大元素 II

    给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它 ...

  6. LeetCode 503. 下一个更大元素 II(Next Greater Element II)

    503. 下一个更大元素 II 503. Next Greater Element II 题目描述 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 ...

  7. [LeetCode] Next Greater Element II 下一个较大的元素之二

    Given a circular array (the next element of the last element is the first element of the array), pri ...

  8. [Swift]LeetCode503. 下一个更大元素 II | Next Greater Element II

    Given a circular array (the next element of the last element is the first element of the array), pri ...

  9. [LeetCode] 496. Next Greater Element I 下一个较大的元素 I

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

随机推荐

  1. CentOS7.4搭建GitLab

    1.查看服务器环境 uname -a 2.下载安装包 [1]找到相应的最新版本的下载路径 网址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/e ...

  2. JavaScript函数体系

    第4章  JavaScript函数 1. 函数基本介绍 ① 为什么需要函数 函数最大的好处就是将零散的代码封装到了一起,当我们要再次使用该功能的时候,不需要再重新书写代码,只需要调用封装好的函数就可以 ...

  3. net start命令发生系统错误5和错误1058的解决方法

    net start命令用于开启服务,格式为:net start [服务名](与之对应的"net stop [服务名]"为关闭服务命令) 5是没有管理员权限,右键管理员即可 1058 ...

  4. ASP.NET 中Textbox只能输入数字,不能输入其他字符

    解决办法如下:  <asp:TextBox ID="txtpage" runat="server" Width="61px"     ...

  5. SpringMVC07SelfException 自定义异常

    1.配置web.xml文件 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3// ...

  6. webpack4流程笔记

    初始化 mkdir webpack-demo   ->新建文件夹  cd webpack-demo  ->进入文件夹 第一步 npm init -y  -> 初始化项目(生成pack ...

  7. HTML5 参数传递

    页面显示效果,如下图: 主页面代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  8. Java笔记--动态代理

    Java动态代理 1.概念 代理: 有时我们并不想直接访问对象A,或者不能直接访问对象A.而是通过访问一个中间对象B,让中间对象B去访问A.这种方式就称为代理. 这里的对象A所属的类就为委托类,或者被 ...

  9. 《Unity預計算即時GI》笔记:二、光照图

    说明 这篇文章是对<Unity預計算即時GI>这个系列文章的笔记. 光照图 什么是光照图 光照图在第三章中有如下的定义,读起来很是费解. 一個光照圖(Chart)是表示一個光照貼圖的區域, ...

  10. C#使用Aspose.Words操作word文档

    最近接到个需求,由于客服这边要导出大量有一定规则的word文件,里面的内容希望系统自动填充,例如 这里我使用Aspose.Words.dll这个类库, 1.首先,我们需要创建模板文件,毕竟有规则的东西 ...