▶ 给定一个数组与它的一个子列,对于数组中的一个元素,定义它右边第一个比他大的元素称为他的后继,求所给子列的后继构成的数组

▶ 第 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的更多相关文章

  1. 【LeetCode】556. Next Greater Element III 解题报告(Python)

    [LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  2. 循序渐进VUE+Element 前端应用开发(30)--- ABP后端和Vue+Element前端结合的分页排序处理

    在很多列表展示数据的场合中,大多数都会需要一个排序的处理,以方便快速查找排序所需的数据,本篇随笔介绍如何结合ABP后端和Vue+Element前端结合的分页排序处理过程. 1.Vue+Element前 ...

  3. [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 ...

  4. 556. Next Greater Element III下一个更大的数字

    [抄题]: Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exac ...

  5. 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 ...

  6. LeetCode 496. 下一个更大元素 I(Next Greater Element I) 35

    496. 下一个更大元素 I 496. Next Greater Element I 题目描述 给定两个没有重复元素的数组 nums1 和 nums2,其中 nums1 是 nums2 的子集.找到  ...

  7. [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 ...

  8. [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 ...

  9. LeetCode 496 Next Greater Element I 解题报告

    题目要求 You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset ...

随机推荐

  1. WPF特效和例子

    https://www.cnblogs.com/AaronYang/p/4710428.html

  2. Rails 5 Test Prescriptions 最后一章,如何测试继承下来的代码,legacy code

    Set expectations 你不可能把一个老旧的代码野兽只用一晚就转变成优雅的奇迹marvel.你需要如下做法: 让自己有好的状态,用15分钟挥舞拳头诅咒之前的程序员 开始工作,这个codeba ...

  3. KMP与AC自动机

    KMP算法主要思想就是预处理出失配函数, 从而减少匹配失败时的回溯, 复杂度是$\Theta(m+n)$, 已达到理论下界 c++代码如下 int n, f[N]; char t[N], p[N]; ...

  4. C#删除图片问题

    public Image GetImage(string path) { FileStream fs = new FileStream(path, FileMode.Open, FileAccess. ...

  5. 牛客网——C列一列

    链接:https://www.nowcoder.net/acm/contest/71/C来源:牛客网 题目描述 小W在计算一个数列{An},其中A1=1,A2=2,An+2=An+1+An.尽管他计算 ...

  6. 114. Flatten Binary Tree to Linked List -- 将二叉树转成链表(in-place单枝树)

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  7. 045——VUE中组件之父组件使用scope定义子组件模板结构

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Linux安装MySQL遇到的问题

    安装: https://www.cnblogs.com/fnlingnzb-learner/p/5830622.html https://www.cnblogs.com/xinjing-jingxin ...

  9. 传智:自己简单实现一个struts2框架的demo

    struts2的结构图: 代码实现: 组织结构: 主要代码: package cn.itcast.config; import org.apache.log4j.Logger; import org. ...

  10. IOS下使用多线程

    ios有三种主要方法:1.NSThread.2.NSOperation.3.GCD. 1.  NSThread: 调用方法如下:如果需要函数参数的话,可以通过Object传递. 1.1:[NSThre ...