2.17 ✅ Sliding Window

Sliding Window #

  • 双指针滑动窗口的经典写法。右指针不断往右移,移动到不能往右移动为止(具体条件根据题目而定)。当右指针到最右边以后,开始挪动左指针,释放窗口左边界。第 3 题,第 76 题,第 209 题,第 424 题,第 438 题,第 567 题,第 713 题,第 763 题,第 845 题,第 881 题,第 904 题,第 978 题,第 992 题,第 1004 题,第 1040 题,第 1052 题。
	left, right := 0, -1

	for left < len(s) {
		if right+1 < len(s) && freq[s[right+1]-'a'] == 0 {
			freq[s[right+1]-'a']++
			right++
		} else {
			freq[s[left]-'a']--
			left++
		}
		result = max(result, right-left+1)
	}
  • 滑动窗口经典题。第 239 题,第 480 题。
No.TitleSolutionDifficultyTimeComplexitySpaceComplexityFavoriteAcceptance
0003Longest Substring Without Repeating CharactersGoMediumO(n)O(1)❤️31.6%
0076Minimum Window SubstringGoHardO(n)O(n)❤️36.2%
0239Sliding Window MaximumGoHardO(n * k)O(n)❤️44.8%
0395Longest Substring with At Least K Repeating CharactersGoMedium43.7%
0424Longest Repeating Character ReplacementGoMediumO(n)O(1)48.4%
0480Sliding Window MedianGoHardO(n * log k)O(k)❤️38.9%
0567Permutation in StringGoMediumO(n)O(1)❤️44.5%
0978Longest Turbulent SubarrayGoMediumO(n)O(1)❤️46.7%
0992Subarrays with K Different IntegersGoHardO(n)O(n)❤️50.9%
0995Minimum Number of K Consecutive Bit FlipsGoHardO(n)O(1)❤️50.1%
1004Max Consecutive Ones IIIGoMediumO(n)O(1)60.9%
1040Moving Stones Until Consecutive IIGoMediumO(n log n)O(1)❤️54.2%
1052Grumpy Bookstore OwnerGoMediumO(n log n)O(1)55.9%
1074Number of Submatrices That Sum to TargetGoHardO(n^3)O(n)❤️62.1%
1208Get Equal Substrings Within BudgetGoMedium44.3%
1423Maximum Points You Can Obtain from CardsGoMedium46.9%
1438Longest Continuous Subarray With Absolute Diff Less Than or Equal to LimitGoMedium44.4%
1658Minimum Operations to Reduce X to ZeroGoMedium33.3%
——————————————————————-——-—————-—————————-————-————-

⬅️上一页

下一页➡️

Calendar Apr 13, 2021
Edit Edit this page
本站总访问量:  次 您是本站第  位访问者