最长连续递增序列-贪心算法-基于Go
给定一个未经排序的整数数组,找到最长且连续递增的子序列,并返回该序列的长度。
如:
[1,2,3,2,3,4,3,4,5,6,7]
返回5.
- 贪心算法
func findLength(nums []int)int{
var start = 0
var maxLength = 0
for i:=1;i<len(nums);i++{
if nums[i]<=nums[i-1]{
start = i
}
maxLength = int(math.Max(float64(maxLength),
float64(i-start+1)))
}
return maxLength
}
- 双指针
func findLength2(nums []int)int{
var low = 0
var high = 1
var n = len(nums)
var maxLength = 0
for low<n && high<n{
if nums[high]<=nums[high-1]{
low = high
}
maxLength = int(math.Max(float64(high-low+1),
float64(maxLength)))
high++
}
return maxLength
}