题目:
n个整数的无序数组,找到每个元素后面比它大的第一个数,要求时间复杂度为 O(n)
思路:
用栈来保存未找到右边第一个比它大的元素的索引(保存索引是因为后面需要靠索引来给新数组赋值),初始时,栈为空。
C++代码实现:
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
vector<int> findMax(vector<int>num)
{
if(num.size()==0)
return num;<