leetcode503
public class Solution {
public int[] NextGreaterElements(int[] nums) {
int n = nums.Length;
int[] next = new int[n];
for (int i = ; i < n; i++)
{
next[i] = -;
}
Stack<int> stack = new Stack<int>(); // index stack
for (int i = ; i < n * ; i++)
{
int num = nums[i % n];
while (stack.Count > && nums[stack.Peek()] < num)
{
next[stack.Pop()] = num;
}
if (i < n)
{
stack.Push(i);
}
}
return next;
}
}
https://leetcode.com/problems/next-greater-element-ii/#/solutions
关键的思路是将数组延长到2倍的长度,然后用%运算,将索引缩小到1倍之内进行1倍长度内的判断。
leetcode503的更多相关文章
- [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 503. 下一个更大元素 II(Next Greater Element II)
503. 下一个更大元素 II 503. Next Greater Element II 题目描述 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 ...
随机推荐
- CSS同时使用背景图片和背景颜色
background:url(../images/bg.jpg) #F3EFE5 no-repeat ;
- php 中的 Output Control 函数
先看一个简单的例子 <?php ob_start(); echo 111; ob_clean(); echo 222; ob_start()开启ob缓存,然后111放进了ob缓存, 再调用ob_ ...
- direct2d封装
图片项目
- pid参数调节的几句话
如果参数上升太快,降低Kp值,如果震荡太剧烈(振荡幅度过大),降低Ki值,如果曲线震荡部分上升下降太快则尝试调整Kd值.
- 【DUBBO】 Dubbo内核实现之动态编译
转载:http://blog.csdn.net/quhongwei_zhanqiu/article/details/41577483 我们运行的java代码,一般都是编译之后的字节码.Dubbo为了实 ...
- 使用C#和Java发送邮件(转载)
using System.Net.Mail; using System.Net; public class EmailEntity { private MailMessage mm; /// < ...
- c++ 中 毫秒级时间获取
#include <time.h> clock_t start,ends; start=clock(); Sleep(); ends=clock(); cout<<ends-s ...
- bzoj 4407 于神之怒加强版——反演
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4407 \( ans = \sum\limits_{D=1}^{min(n,m)}\frac{ ...
- sprinboot+redis
(1)pom.xml引入jar包,如下: <dependency> <groupId>org.springframework.boot</groupId> < ...
- kubectl get pods The connection to the server was refused - did you specify the rig
1 主要是运行这个命令 alias kubectl='kubectl --kubeconfig=/etc/kubernetes/kubelet.conf'问题解决. 同时也用到如下命令: pas ...