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. 【Java密码学】XML签名

    http://www.oracle.com/technetwork/articles/javase/dig-signature-api-140772.html XML签名的结构和类型 基本上XML签名 ...

  2. Nginx支持PHP的CI框架

    1.找到CI库的配置文件修改 $config['base_url']    = 'http://test.example.com'; $config['uri_protocol'] = 'PATH_I ...

  3. C# EF 生成1-20随机数,生成10个,不能重复 LINQ写法

    , ).Select(x => ).ToList(); foreach (var i in num) { Console.WriteLine(i); } 上面的函数碉堡天了

  4. spring数组注入

    数组注入 public class MyCollection {     private  String[]array;     private List<String>list;     ...

  5. elasticsearch远程代码执行漏洞告警

    es版本:1.7.2 最近在做es项目的时候出现,启动es一段时间系统就会报警,结果查询了一下,原来是es的漏洞: 官网描述: 大致意思就是: 漏洞出现在脚本查询模块,默认搜索引擎支持使用脚本代码(M ...

  6. mysql set names 命令和 mysql字符编码问题

    先看下面的执行结果: (root@localhost)[(none)]mysql>show variables like 'character%'; +--------------------- ...

  7. HTTP原理及状态码汇总

    什么是HTTP: HTTP(HyperText Transfer Protocol超文本传输协议)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准,为了提供一种发布和接收HTM ...

  8. ABAP WRITE

    1.空行 WRITE /. 2.AS CHECKBOX VALUE 'X', check2 VALUE ' '. START-OF-SELECTION. WRITE: / check1 AS CHEC ...

  9. 一篇文章读懂JSON

    什么是json? W3C JSON定义修改版: JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式,并不是 ...

  10. 阻止Bootstrap 模态框(Modal)点击空白处时关闭

    默认情况下点击空白处时会关闭模态框,添加data-backdrop="static"后可以阻止关闭