最长连续递增序列-贪心算法-基于Go

时间:2021-04-09 12:51:45   收藏:0   阅读:0

给定一个未经排序的整数数组,找到最长且连续递增的子序列,并返回该序列的长度。

如:
[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
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!