Skip to content

Latest commit

 

History

History
47 lines (42 loc) · 926 Bytes

04_sliding_window.md

File metadata and controls

47 lines (42 loc) · 926 Bytes

滑动窗口专题

题目汇总

  • leetcode_0003
  • leetcode_0076
  • leetcode_0159(PLUS)
  • leetcode_0209
  • leetcode_0219
  • leetcode_0340(PLUS)
  • leetcode_0395
  • leetcode_0424
  • leetcode_0992

解题模版

// leetcode_0076
func minSubArrayLen(target int, nums []int) int {
	size := len(nums)
	var sum int

	minLength := 1 << 15
	left, right := 0, 0
	// 1. 持续右移 right
	for right=0; right < size; right ++ {
		sum += nums[right]
		// 2. 持续左移 left
		for sum >= target {
			currLength := right - left + 1
			// 3. 处理满足条件的结果
			if currLength < minLength {
				minLength = currLength
			}
			// 4. 找到部分结果后,继续向后寻找
			sum -= nums[left]
			left++
		}
	}

	if minLength == 1 << 15 {
		return 0
	}

	return minLength
}

专题总结