[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), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn't exist, output -1 for this number.
Example 1:
Input: [1,2,1]
Output: [2,-1,2]
Explanation: The first 1's next greater number is 2;
The number 2 can't find next greater number;
The second 1's next greater number needs to search circularly, which is also 2.
Note: The length of given array won't exceed 10000.
496. Next Greater Element I 的拓展,这里的数组是循环的,某一个元素的下一个较大值可以在其前面。
解法:栈,与496的不同是:循环是2倍的数组长度,用栈来保存降序序列的index。
Java:
public int[] nextGreaterElements(int[] nums) {
int n = nums.length, next[] = new int[n];
Arrays.fill(next, -1);
Stack<Integer> stack = new Stack<>(); // index stack
for (int i = 0; i < n * 2; i++) {
int num = nums[i % n];
while (!stack.isEmpty() && nums[stack.peek()] < num)
next[stack.pop()] = num;
if (i < n) stack.push(i);
}
return next;
}
Python:
def nextGreaterElements(self, nums):
stack, res = [], [-1] * len(nums)
for i in range(len(nums)) * 2:
while stack and (nums[stack[-1]] < nums[i]):
res[stack.pop()] = nums[i]
stack.append(i)
return res
C++:
vector<int> nextGreaterElements(vector<int>& nums) {
int n = nums.size();
vector<int> next(n, -1);
stack<int> s; // index stack
for (int i = 0; i < n * 2; i++) {
int num = nums[i % n];
while (!s.empty() && nums[s.top()] < num) {
next[s.top()] = num;
s.pop();
}
if (i < n) s.push(i);
}
return next;
}
类似题目:
[LeetCode] 496. Next Greater Element I 下一个较大的元素 I
[LeetCode] 556. Next Greater Element III 下一个较大的元素 III
All LeetCode Questions List 题目汇总
[LeetCode] 503. Next Greater Element II 下一个较大的元素 II的更多相关文章
- [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 ...
- [LeetCode] 556. Next Greater Element III 下一个较大的元素 III
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...
- Leetcode496.Next Greater Element I下一个更大的元素1
给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值. nums1 中数字 x 的下一个更 ...
- [leetcode]496. Next Greater Element I下一个较大元素
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- [LeetCode] Next Greater Element II 下一个较大的元素之二
Given a circular array (the next element of the last element is the first element of the array), pri ...
- 496 Next Greater Element I 下一个更大元素 I
给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值.nums1 中数字 x 的下一个更大 ...
- [LeetCode] Next Greater Element III 下一个较大的元素之三
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...
- [LeetCode] Next Greater Element I 下一个较大的元素之一
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- 556. Next Greater Element III下一个更大的数字
[抄题]: Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exac ...
随机推荐
- 项目(一)--python3--爬虫实战
最近看了python3网络爬虫开发实战一书,内容全面,但不够深入:是入门的好书. 作者的gitbook电子版(缺少最后几章) python3网络爬虫实战完整版PDF(如百度网盘链接被屏蔽请联系我更新) ...
- 网络基础知识(http请求)
http请求的过程 域名解析----TCP连接 ----发送请求-----响应请求----获取html代码----浏览器渲染 TCP是主机对主机层的控制传输协议,提供可靠的连接服务 TCP的三次握手 ...
- java-we不在esclipse创建servlet之后改名不起作用的问题归纳
有时候我们不满意类名而去改名,但是改过了之后却发现不能实现它本来该实现的功能了,这是为什么呢,原因就是在2.5里面创建了servlet之后就会在web.xml里生成关于这个servlet的配置,你只是 ...
- SparkSQL读写外部数据源--csv文件的读写
object CSVFileTest { def main(args: Array[String]): Unit = { val spark = SparkSession .builder() .ap ...
- K3二次开发后台表
select * from icclasstype where fname_chs like '%供货%' 用此表基本上可以查询到所有的表 select * from POrequest --采购申请 ...
- LeetCode 802. Find Eventual Safe States
原题链接在这里:https://leetcode.com/problems/find-eventual-safe-states/ 题目: In a directed graph, we start a ...
- 关于windbg报错"No symbols for ntdll. Cannot continue."问题
最近我写个例子程序研究下某个异常情况,故意制造了个崩溃.然后分析dmp文件. 当我执行!address -summary命令想观察下进程当前内存情况时,去报如下错误: 0:000> !addre ...
- 使用merge-graphql-schemas 进行graphql schema 以及resovler 合并
merge-graphql-schemas 是一个方便的工具,可以进行schema 以及resovler 的合并处理 一个schema 合并参考demo schema 定义 // ./graphql/ ...
- 微信小程序搜索框代码组件
search.wxml <view class="header"> <view class="search"> <icon typ ...
- vue使用axios发送请求,都会发送两次请求
vue 使用axios,每次的请求都会发送两次,第一次的请求头为options CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sha ...