503. Next Greater Element II
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的更多相关文章
- [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 ...
- 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 ...
- 【LeetCode】503. Next Greater Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 单调递减栈 日期 题目地址:https:/ ...
- 496. Next Greater Element I + 503. Next Greater Element II + 556. Next Greater Element III
▶ 给定一个数组与它的一个子列,对于数组中的一个元素,定义它右边第一个比他大的元素称为他的后继,求所给子列的后继构成的数组 ▶ 第 496 题,规定数组最后一个元素即数组最大元素的后继均为 -1 ● ...
- 503 Next Greater Element II 下一个更大元素 II
给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它 ...
- LeetCode 503. 下一个更大元素 II(Next Greater Element II)
503. 下一个更大元素 II 503. Next Greater Element II 题目描述 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 ...
- [LeetCode] Next Greater Element II 下一个较大的元素之二
Given a circular array (the next element of the last element is the first element of the array), pri ...
- [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 ...
- [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 ...
随机推荐
- Apache禁止访问网站子目录的方法
在PHP网站开发中,基于WEB服务器和PHP网站程序代码的安全考虑,我们需要对相关的目录或者文件访问权限进行控制,以防止意外情况的发生,那么我们如何来实现这种功能呢?我们可以通过Apache来实现禁止 ...
- MySQL导入大sql 文件大小限制问题的解决
解决过程如下: 1.由于mysql能解析sql的压缩文件,因此将200M压缩后为5M. 2.默认情况下:MySQL导入文件大小有限制的,最大为2M,所以当文件很大时候,直接无法导入,可修改php.in ...
- [luogu 3369]普通平衡树(fhq_treap)
题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(排名定义为比当前数小的数的个数+1.若有多 ...
- <邮件的反垃圾反病毒>
本章——发送接收邮件的工具为雷鸟 安装 # yum install dovecot-mysql.x86_64 dovecot.x86_64 -y 编辑文件 vim 10-mail.conf mail_ ...
- vscode 插件todo-highlight
简介 Highlight TODO,FIXME or any annotations within your code. 资源 https://marketplace.visualstudio.com ...
- 移植mavlink协议到STM32详细教程
1准备材料, 首先准备一个带串口的stm32程序(这里选用整点原子的官方串口例程这里自己去找不讲)作者:恒久力行 QQ:624668529,然后去mavlink官网下载mavlink源码,这里重点讲解 ...
- Mongodb简介及基本操作
一.简介 MongoDB是一款强大.灵活.且易于扩展的通用型数据库 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性 ...
- Python学习笔记-day1(if流程控制)
在python中,流程控制语句为强制缩进(4空格) if username=='lmc' and password=='123456': print('Welcome User {name} logi ...
- linux 命令——34 du(转)
Linux du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的. 1.命令格式: du [选项][文件] 2.命令功能 ...
- Problem A: C语言习题 链表建立,插入,删除,输出
#include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct student { l ...