Skip to content

Commit 8834023

Browse files
authored
Create (数组操作)674、最长连续递增序列
1 parent 9967fff commit 8834023

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
给定一个未经排序的整数数组,找到最长且连续的的递增序列。
3+
4+
示例 1:
5+
6+
输入: [1,3,5,4,7]
7+
输出: 3
8+
解释: 最长连续递增序列是 [1,3,5], 长度为3。
9+
尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为5和7在原数组里被4隔开。
10+
11+
示例 2:
12+
13+
输入: [2,2,2,2,2]
14+
输出: 1
15+
解释: 最长连续递增序列是 [2], 长度为1。
16+
17+
注意:数组长度不会超过10000。
18+
*/
19+
20+
int findLengthOfLCIS(int* nums, int numsSize)
21+
{
22+
int current = 1;
23+
int max = 1;
24+
if(numsSize <= 1)
25+
return numsSize;
26+
27+
for(int i = 0; i < numsSize - 1; i++)
28+
{
29+
if(nums[i] < nums[i+1])
30+
current++;
31+
else
32+
current = 1;
33+
max = max > current ? max : current;
34+
}
35+
36+
return max;
37+
}
38+
39+
/*
40+
由于只是返回最长连续子序列的长度,不要求返回子序列,所以题目相对简单一些。
41+
定义current变量用来记录当前遍历下的连续递增序列的元素个数,max变量用来记录
42+
所遍历过的连续递增子序列中的最长序列的序列长度。
43+
对于序列遍历过程中,如果右侧元素大于左侧元素,则current自增,一旦出现非递增
44+
情况,current变为1,并将之前遍历的结果与max记录的结果进行比较,使max只记录
45+
最长的情况。
46+
遍历完数组之后,返回max即可。
47+
*/

0 commit comments

Comments
 (0)