您的位置:首页 > 产品设计 > UI/UE

674-Longest Continuous Increasing Subsequence

2017-09-24 19:10 399 查看
难度:easy

类别:array

1.题目描述

Input: [1,3,5,4,7]

Output: 3

Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3.

Even though [1,3,5,7] is also an increasing subsequence, it’s not a continuous one where 5 and 7 are separated by 4.

2.算法分析

这道题目与前面寻找最大子数组的和有点相似,同样要讲最大长度的递增连续子数组的长度存储起来;直接用一个for循环不断地进行判断即可,实现相对简单。

3.代码实现

int findLengthOfLCIS(vector<int>& nums) {
int n = nums.size();
if (n == 0) return 0;
if (n == 1) return 1;
int count = 1;
//  save the longest size in max
int max = count;
for (int i = 0; i < n - 1; ++i) {
if (nums[i + 1] > nums[i]) {
count++;
} else {
count = 1;
}
if (count > max) {
max = count;
}
}
return max;
}


main函数,用于测试:

#include <iostream>
#include <vector>
using namespace std;

int findLengthOfLCIS(vector<int>& nums);
int main() {
int n, value;
cout << "array size: ";
cin >> n;
vector<int> nums;
for (int i = 0; i < n; ++i) {
cin >> value;
nums.push_back(value);
}
cout << findLengthOfLCIS(nums) << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode LCIS 674 array