496. Next Greater Element I + 503. Next Greater Element II + 556. Next Greater Element III
▶ 给定一个数组与它的一个子列,对于数组中的一个元素,定义它右边第一个比他大的元素称为他的后继,求所给子列的后继构成的数组
▶ 第 496 题,规定数组最后一个元素即数组最大元素的后继均为 -1
● 自己的版本,12 ms,最快的解法算法与之相同
class Solution
{
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums)
{
const int m = findNums.size(), n = nums.size();
int i, j;
unordered_map<int, int> table;
vector<int> output;
for (i = ; i < n; table[nums[i]] = i, i++);
for (i = ; i < m; i++)
{
if (findNums[i] == nums[n - ])
{
output.push_back(-);
continue;
}
for (j = table[findNums[i]] + ; j < n && nums[j] < findNums[i]; j++);
if (j == n)
output.push_back(-);
else
output.push_back(nums[j]);
}
return output;
}
};
▶ 第 503 题,数组换成环状,只有数组最大元素的后继为 -1
● 自己的代码,124 ms,添加一个标记 mark 的线性搜索方法
class Solution
{
public:
vector<int> nextGreaterElements(vector<int>& nums)
{
const int n = nums.size();
int i, mark;
vector<int> output(n, -);
for (mark = ; mark < n - && nums[mark] <= nums[n - ]; mark++);
if (mark < n - ) // 找到了某个位置,标记上,若 mark == n - 1,说明 nums[n - 1] 就是最大的
output[n - ] = nums[mark];
for (i = n - ; i >= ; i--)
{
if (nums[i] < nums[i + ])
{
output[i] = nums[i + ];
mark = i + ;
}
else
{
for (; mark != i && nums[mark] <= nums[i]; mark = (mark + ) % n);
if (mark != i)
output[i] = nums[mark];
}
}
return output;
}
};
● 大佬的代码,111 ms
class Solution
{
public:
vector<int> nextGreaterElements(vector<int>& nums)
{
const int n = nums.size();
int i, num, ind;
stack<int> st; // 栈维护 num 的单调递减的子列的下标,遇到较大的当前值的时候出栈,出到元素值大于当前值为止
vector<int> result(n, -);
for (i = ; i < * n; i++)// 扫描两趟,确保末尾的元素找到后继
{
for (num = nums[i % n]; !st.empty() && num > nums[st.top()]; ind = st.top(), st.pop(), result[ind] = num);
// 若 num 较大,则把栈中的较小元素都指向 num
st.push(i % n); // 空栈,或者剩余部分的值大于当前值,将当前值压栈
}
return result;
}
};
● 大佬的代码,103 ms,两步走战略
class Solution
{
public:
vector<int> nextGreaterElements(vector<int> nums)
{
const int n = nums.size();
if(n==)
return vector<int>();
vector<int> res(n);
int i, maxValue, index, real;
for (maxValue = nums[], i = index = ; i < n; i++)// 第一轮,将两种情况的第 k 元素进行标记:
{ // ① nums[ k ] < nums[ k + 1 ]
if (nums[i] > maxValue) // ② nums[ k ] >= nums[ k + 1 ] ... nums[ k + m ] 且 nums[ k ] < nums[ k + m ]
{ // 注意第一轮结束时 index 等于数组最大元素的下标
res[i - ] = i;
res[index] = i;
maxValue = nums[i];
index = i;
}
}
for (res[index] = -, i = index - ; i > index - n; i--)// 将数组最大元素的输出值置 -1,从最大元素开始向左遍历数组
{
real = (i + n) % n;
if (res[real] != )
continue;
res[real] = (real == n - ? : real + );// 让该元素的输出值初始化为它右边那个元素
for (; res[real] != - && nums[real] >= nums[res[real]]; res[real] = res[res[real]]);// 寻找该元素的真实输出值
}
for (i = ; i < n; i++)// 将下标置换回元素的值
{
if (res[i] != -)
res[i] = nums[res[i]];
}
return res;
}
};
● 其他方法,枚举,时间复杂度 O(n2)
496. Next Greater Element I + 503. Next Greater Element II + 556. Next Greater Element III的更多相关文章
- 【LeetCode】556. Next Greater Element III 解题报告(Python)
[LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 循序渐进VUE+Element 前端应用开发(30)--- ABP后端和Vue+Element前端结合的分页排序处理
在很多列表展示数据的场合中,大多数都会需要一个排序的处理,以方便快速查找排序所需的数据,本篇随笔介绍如何结合ABP后端和Vue+Element前端结合的分页排序处理过程. 1.Vue+Element前 ...
- [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 ...
- 556. Next Greater Element III下一个更大的数字
[抄题]: Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exac ...
- 556. 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 496. 下一个更大元素 I(Next Greater Element I) 35
496. 下一个更大元素 I 496. Next Greater Element I 题目描述 给定两个没有重复元素的数组 nums1 和 nums2,其中 nums1 是 nums2 的子集.找到 ...
- [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] 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 解题报告
题目要求 You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset ...
随机推荐
- 浅谈Android Studio3.0更新之路(遇坑必入)
>可以参考官网设置-> 1 2 >> Fantasy_Lin_网友评论原文地址是:简书24K纯帅豆写的我也更新一下出处[删除]Fa 转自脚本之家 浅谈Android Studi ...
- 在python中引用jar包
在本文中,首先创建一个maven项目,然后打包成jar包,在python中调用jar包,执行java类方法. 1.在eclipse中创建maven项目 我这里创建一个普通java项目,择选择 “mav ...
- 2-7-搭建DNS服务器实现域名解析
学习服务的方法: 了解服务的作用:名称,功能,特点 安装服务 配置文件的位置,端口 服务开启和关闭的脚本 修改配置文件(实战举例) 排错(从上到下,从内到外) -------------------- ...
- python运行错误------Non-UTF-8 code
1.安装-----见:https://www.cnblogs.com/weven/p/7252917.html 本文转载于:http://blog.csdn.net/youyuyixiu/articl ...
- Android orm 框架xUtils简介
数据库操作建议用ORM框架,简单高效.这里推荐xUtils,里面包含DBUtils.github地址:https://github.com/wyouflf/xUtils 获得数据库实例建议用单例模式. ...
- java读取和写入浏览器Cookies
首先我们认识下什么是cookies: cookie实际上是一个存在你硬盘里的数据,但是这些数据很特殊,只能由web应用提交给浏览器帮助存储,并且我们还能读取浏览器存在本地的cookie web应用一般 ...
- JavaScript学习总结(二十二)——JavaScript屏蔽Backspace键
今天在IE浏览器下发现,当把使用readonly="readonly"属性将文本框设置成只读<input type="text" readonly=&qu ...
- itembase协同过滤的详细介绍
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:https://www.cnblogs.com/by-dream/p/9016289.html 前言 通常我们在网购的时候会遇到这 ...
- docker把web jar包制作成镜像
1.新建一个spring boot项目 并使用maven打成jar包,放到linux(centos7)环境上. 运行 java -jar hello.jar 后结果如下(这里项目对外提供的端口是90 ...
- New Concept English Two 10 25
$课文23 新居 219. I had a letter from my sister yesterday. 昨天我收到了姐姐的一封信, 220. She lives in Nigeria. 她住在尼 ...