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

▶ 第 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. 查找一个Class到底在那一个jar文件里

    整理自己的一些笔记,发觉这个命令 ,看起来是用来找一个Class到底在那一个jar文件里的. 虽然没有再测一下,估计是好使的. 先在博客园里记下来,防止自己忘掉. findstr /S /M org. ...

  2. find ... -exec ... {} \; 的解释

    find的特殊功能是能够进行额外的动作,如上图的 find / -type f -name "test.txt" -exec rm {} \;命令 1) {} 代表的是由find找 ...

  3. spring boot 使用velocity模板(十六)

    (不要使用这种模板了,spring boot最新版已经不支持了.使用FreeMarker吧:http://blog.csdn.net/clementad/article/details/5194262 ...

  4. html中元素盒子垂直居中的实现方法

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. C++设计模式之桥接模式

    [DP]书上定义:将抽象部分与它的实现部分分离,使它们都可以独立地变化.考虑装操作系统,有多种配置的计算机,同样也有多款操作系统.如何运用桥接模式呢?可以将操作系统和计算机分别抽象出来,让它们各自发展 ...

  6. IDEA12使用初体验

    最近将开发工具IDEA升级到了12.0版本,被它新的UI界面深深吸引,看下面,很酷哦~ 一.下载安装 在IDEA官网下载最近版本12.0,有免费的社区版,还有收费的无限制版,大家可以自行下载. 下载后 ...

  7. maven打包资源文件(转)

    原文链接:http://blog.csdn.net/u012849872/article/details/51035938 maven工程标准目录结构: src    -main       –bin ...

  8. zepto 的 css 方法 -- 待续

    链接 获取样式:  getComputedStyle  什么是计算后的样式 就是经过css样式组合 和 js操作后 的 最后的结果 设置样式有三种方法: div.style.backgroundCol ...

  9. Winform开发之DataGridView的增删改

    DataGridView是一个非常强大的控件,用法很多.这里介绍一个简单的增删改例子. 贴效果图 右侧输入学生信息点击新增,将数据增加到数据库,并且加载到datagridview中,点击选择某条数据修 ...

  10. ZetCode PyQt4 tutorial Drag and Drop

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This is a simple ...