[LeetCode] Next Greater Element 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.
这道题是之前那道Next Greater Element I的拓展,不同的是,此时数组是一个循环数组,就是说某一个元素的下一个较大值可以在其前面,那么对于循环数组的遍历,为了使下标不超过数组的长度,我们需要对n取余,下面先来看暴力破解的方法,遍历每一个数字,然后对于每一个遍历到的数字,遍历所有其他数字,注意不是遍历到数组末尾,而是通过循环数组遍历其前一个数字,遇到较大值则存入结果res中,并break,再进行下一个数字的遍历,参见代码如下:
解法一:
- class Solution {
- public:
- vector<int> nextGreaterElements(vector<int>& nums) {
- int n = nums.size();
- vector<int> res(n, -);
- for (int i = ; i < n; ++i) {
- for (int j = i + ; j < i + n; ++j) {
- if (nums[j % n] > nums[i]) {
- res[i] = nums[j % n];
- break;
- }
- }
- }
- return res;
- }
- };
我们可以使用栈来进行优化上面的算法,我们遍历两倍的数组,然后还是坐标i对n取余,取出数字,如果此时栈不为空,且栈顶元素小于当前数字,说明当前数字就是栈顶元素的右边第一个较大数,那么建立二者的映射,并且去除当前栈顶元素,最后如果i小于n,则把i压入栈。因为res的长度必须是n,超过n的部分我们只是为了给之前栈中的数字找较大值,所以不能压入栈,参见代码如下:
解法二:
- class Solution {
- public:
- vector<int> nextGreaterElements(vector<int>& nums) {
- int n = nums.size();
- vector<int> res(n, -);
- stack<int> st;
- for (int i = ; i < * n; ++i) {
- int num = nums[i % n];
- while (!st.empty() && nums[st.top()] < num) {
- res[st.top()] = num; st.pop();
- }
- if (i < n) st.push(i);
- }
- return res;
- }
- };
类似题目:
参考资料:
https://discuss.leetcode.com/topic/77871/short-ac-solution-and-fast-dp-solution-45ms
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 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] 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 ...
- 503 Next Greater Element II 下一个更大元素 II
给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 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 ...
- Leetcode496.Next Greater Element I下一个更大的元素1
给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值. nums1 中数字 x 的下一个更 ...
- 496 Next Greater Element I 下一个更大元素 I
给定两个没有重复元素的数组 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 ...
随机推荐
- WEB端线上偶现问题如何复现?
1.抓取出现问题的日志,还原操作过程,分析 每个过程中数据是否正常?是否有重复请求 2.询问当时操作员执行了哪些操作,尽可能多的了解事发经过 3.通过查看日志,数据库等信息,找到发生问题的节点, 比如 ...
- JavaOOP-集合框架
1.Java集合框架包含的内容 Java集合框架为我们提供了一套性能优良,使用方便的接口和类,它们都位于在java.util包中. Collection 接口存储一组不唯一,无序的对象. List 接 ...
- Dynamics 365 for CRM:CRM与ADFS安装到同一台服务器,需修改ADFS服务端口号
CRM与ADFS安装到同一台服务器时,出现PluginRegistrationTool 及 CRM Outlook Client连接不上,需要修改ADFS的服务端口号,由默认的808修改为809: P ...
- Java基础学习笔记十 Java基础语法之final、static、匿名对象、内部类
final关键字 继承的出现提高了代码的复用性,并方便开发.但随之也有问题,有些类在描述完之后,不想被继承,或者有些类中的部分方法功能是固定的,不想让子类重写.可是当子类继承了这些特殊类之后,就可以对 ...
- Bean validation
公司测试非常严格,要求我们对每个参数的长度进行校验,提了一个参数长度校验的单,然后我们老大就把我们的代码全部打回去了.... 一个bean类中往往有超多变量,如果一个个写if else,够呛,而且圈复 ...
- 极其简单的帮你理解ORM中的关联关系
ORM对象关系映射(英语:(Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象编程语言里不同类型系统的数据之间的转 ...
- IDEA配置Struts框架
对于刚接触编程的同学,对框架只是还不是很了解,本文主要介绍在Idea上配置Struts,实现简单的页面跳转,以及页面参数传递. 在进行代码编写之前先对Idea进行一个简单了解,对于长时间接触编程的,对 ...
- alpha冲刺第三天
一.合照 二.项目燃尽图 三.项目进展 今天是一个瓶颈期,在昨天被困住的地方今天还是没能解决,所以今天的项目进展并没有发生什么变化. 今天晚上xl和lj去实验室找学姐了,在学姐的帮助下大概有了一点思路 ...
- 学号:201621123032 《Java程序设计》第11周学习总结
1:本周学习总结 1.1.:以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2:书面作业 2.1.: 源代码阅读:多线程程序BounceThread 1.1: BallRunnable类有什 ...
- prototype 原型链
// 方法1 var aa=function(){ function bb(){ this.name="1111"; console.log(this.name) }; bb.pr ...