LeetCode 674. Longest Continuous Increasing Subsequence (最长连续递增序列)
Posted 几米空间
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 674. Longest Continuous Increasing Subsequence (最长连续递增序列)相关的知识,希望对你有一定的参考价值。
Given an unsorted array of integers, find the length of longest continuous
increasing subsequence.
Example 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.
Example 2:
Input: [2,2,2,2,2] Output: 1 Explanation: The longest continuous increasing subsequence is [2], its length is 1.
Note: Length of the array will not exceed 10,000.
题目标签:Array
题目给了一个没有排序的nums array,让我们找到其中最长连续递增序列的长度。
维护一个maxLen,每次遇到递增数字就tempLen++,遇到一个不是递增数字的话,就把tempLen 和maxLen 中大的保存到maxLen。
Java Solution:
Runtime beats 69.72%
完成日期:10/21/2017
关键词:Array
关键点:维护一个maxLen
1 class Solution 2 { 3 public int findLengthOfLCIS(int[] nums) 4 { 5 if(nums == null || nums.length == 0) 6 return 0; 7 8 int maxLen = 0; 9 int tempLen = 1; 10 11 for(int i=1; i<nums.length; i++) 12 { 13 if(nums[i] <= nums[i-1]) 14 { 15 maxLen = Math.max(maxLen, tempLen); 16 tempLen = 1; 17 } 18 else 19 { 20 tempLen++; 21 } 22 23 } 24 25 return Math.max(maxLen, tempLen); 26 } 27 }
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
以上是关于LeetCode 674. Longest Continuous Increasing Subsequence (最长连续递增序列)的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 674. Longest Continuous Increasing Subsequence (最长连续递增序列)
674. Longest Continuous Increasing Subsequence
674. Longest Continuous Increasing Subsequence
674. Longest Continuous Increasing Subsequence