第674题,Longest Continuous Increasing Subsequence
题目:
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).
本题的意思是给一个未排序的数组,求出数组中连续递增的序列的最大长度。
比如:
Input: [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2], its length is 1.
这题的难度比较简单,设置两个变量max_length, lengthCount,从最低索引位开始,依次比较两个数值,如果后一个比前一个大则,lengthCount加一,否则清零,遍历完数组之后便可确定max_length。
源代码如下。
#include<stdio.h>
int findLengthOfLCIS(int* nums,int numSize);
int arr[] = {1,3,5,4,7,8,9,10} ;
int arr1[] = {1,2,1,2,1,2,1,2,1,2,1,1,1,1,1,1,1,1};
int *ptr;
int main(int argc ,char *argv[])
{
int max_length = 0;
printf("\r\n********systerm start **************\r\n");
//test 1
printf("test 1,null ptr\r\n ");
if( 0 == findLengthOfLCIS(ptr,5))
{
printf("error \r\n");
}
//test 2
printf("test 2,lent = 0\r\n ");
if( 0 == findLengthOfLCIS(arr,0))
{
printf("error \r\n");
}
//test 3
printf("test 3,normal test \r\n ");
max_length = findLengthOfLCIS(arr1,15);
printf("max length = %d \r\n",max_length);
printf("\r\n********systerm finish **************\r\n");
return 1;
}
int findLengthOfLCIS(int* nums,int numsSize)
{
int index;
int max_length = 1;
int lengthCount = 1;
if(nums == NULL)
{
return 0;
}
if(numsSize == 0)
{
return 0;
}
for(index = 0 ;index < (numsSize - 1); index++)
{
if(nums[index+1] > nums[index])
{
lengthCount++;
}
else
{
if(max_length < lengthCount)
{
max_length = lengthCount;
}
lengthCount = 1;
}
}
if(max_length < lengthCount)
{
max_length = lengthCount;
}
return max_length;
}