本文主要是介绍leetcode第674题,Longest Continuous Increasing Subsequence,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
第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;}
这篇关于leetcode第674题,Longest Continuous Increasing Subsequence的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!