双指针,后面那个边界条件让我调了好几分钟
func findLHS(nums []int) int {
sort.Ints(nums)
left, right, maxLen := 0, 0, 0
for {
if nums[right] - nums[left] > 1 {
left++
} else if nums[right] - nums[left] < 1 {
right++
} else if nums[right] - nums[left] == 1 {
subLen := right - left + 1
if subLen > maxLen {
maxLen = subLen
}
right++
}
if right > len(nums) - 1 {
right = len(nums) - 1
left++
}
if left >= len(nums) - 1 {
break;
}
}
return maxLen
}
标签:right,594,nums,++,maxLen,序列,leetcode,left
From: https://www.cnblogs.com/wudanyang/p/17018020.html