解题思路: 遍历一遍,保存一个全局的最大长度,复杂度为O(n)
#include<iostream>
#include <vector>
using namespace std;
int longest_subarray(vector<int> data){
int current = 1;
int size = data.size();
int res = 1;
for (int i = 1; i < size; i++)
{
if (data[i]>data[i - 1])
{
current++;
}
else{
if (current > res)
{
res = current;
}
current = 1;
}
}
return res;
}
int main()
{
int data[] = { 2, 4, 3, 6, 3, 2, 5, 5 };
vector<int> vec(data,data+8);
int result = longest_array(vec);
printf("%d\n", result);
getchar();
return 0;
}