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 下一个较大的元素 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 ...
- LeetCode 556. 下一个更大元素 III(Next Greater Element III)
556. 下一个更大元素 III 556. Next Greater Element III 题目描述 给定一个 32 位正整数 n,你需要找到最小的 32 位整数,其与 n 中存在的位数完全相同,并 ...
- Binary search for the first element greater than target
We all know how to search through an array for an element whose value equals the target value, but h ...
随机推荐
- 从国内流程管理软件市场份额看中国BPM行业发展
随着互联网+.中国制造2025.工业4.0等国家战略的支持与引导,企业在数字经济时代的信息化表现惊人,越来越多企业认识到,对于企业的发展来说,信息自动化远远还不够,企业的战略.业务和IT之间需保持高度 ...
- angularjs定义全局变量
angularjs定义全局变量 三种方法 直接外层定义全局变量 利用ng的value定义全局变量 利用ng的constant定义全局变量 Takl is cheap, Show me the code ...
- Windows Server 2012中安装Active Directory域服务
1.登陆Windows Server 2012,打开服务器管理器,选择"添加角色和功能" 2.在"开始之前"页面,直接点击"下一步" 3.选 ...
- NuGet在Push的时候提示“远程服务器返回错误:(403)已禁用”问题解决
在使用NuGet把包push到nuget官网的时候,提示了如下信息: Failed to process request. 'The specified API key is invalid or d ...
- SRM DIV1 500pt DP
SRM 501 DIV1 500pt SRM 502 DIV1 500pt SRM 508 DIV1 500pt SRM 509 DIV1 500pt SRM 511 DIV1 500pt SRM 5 ...
- jQuery UI 日期控件--datepicker
在web开发中,日期的输入经常会遇到.我们会用的解决方法有: 1.自己写css和js,对日期进行控制:----有点浪费精力和时间: 2.用easyui插件中的日期插件来实现: 3.用juqery-ui ...
- angular2 实现的小项目
之前根据官网的demo做了一个小例子,将的都比较基本,为了更好的提高对angular的认知,又做了一个小例子,目前还不完善.主要有路由,http,组件之间的通信,服务等基本知识. 项目地址:https ...
- 剑指offer(65)矩阵中的路径
题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵中 ...
- python 学习笔记 ---- 数据类型
Python有五个标准的数据类型: Numbers(数字) String(字符串) List(列表) Tuple(元组) Dictionary(字典) ① List 列表 和 Tuple 元组 ...
- 分步理解 Promise 的实现
一个 Promise 的运用: var firstPromise = new Promise(function(resolve,reject){ setTimeout(function(){ var ...