2.03 ✅ Two Pointers

Two Pointers #

  • Classic way to write the two-pointer sliding window. The right pointer keeps moving to the right until it can no longer move right (the specific condition depends on the problem). After the right pointer reaches the far right, start moving the left pointer to release the left boundary of the window. Problem 3, Problem 76, Problem 209, Problem 424, Problem 438, Problem 567, Problem 713, Problem 763, Problem 845, Problem 881, Problem 904, Problem 978, Problem 992, Problem 1004, Problem 1040, Problem 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)
	}
  • Fast and slow pointers can be used to find duplicate numbers, with time complexity O(n), Problem 287.
  • After replacing letters, the longest possible length of consecutive identical letters. Problem 424.
  • SUM problem set. Problem 1, Problem 15, Problem 16, Problem 18, Problem 167, Problem 923, Problem 1074.
No.TitleSolutionDifficultyTimeComplexitySpaceComplexityFavoriteAcceptance
0011Container With Most WaterGoMediumO(n)O(1)54.0%
00153SumGoMediumO(n^2)O(n)❤️32.6%
00163Sum ClosestGoMediumO(n^2)O(1)❤️45.8%
00184SumGoMediumO(n^3)O(n^2)❤️35.9%
0019Remove Nth Node From End of ListGoMediumO(n)O(1)41.0%
0026Remove Duplicates from Sorted ArrayGoEasyO(n)O(1)51.5%
0027Remove ElementGoEasyO(n)O(1)53.0%
0028Find the Index of the First Occurrence in a StringGoEasyO(n)O(1)39.0%
0031Next PermutationGoMedium37.5%
0042Trapping Rain WaterGoHardO(n)O(1)❤️59.2%
0061Rotate ListGoMediumO(n)O(1)36.1%
0075Sort ColorsGoMediumO(n)O(1)❤️58.5%
0080Remove Duplicates from Sorted Array IIGoMediumO(n)O(152.3%
0082Remove Duplicates from Sorted List IIGoMedium45.9%
0086Partition ListGoMediumO(n)O(1)❤️52.0%
0088Merge Sorted ArrayGoEasyO(n)O(1)❤️46.6%
0125Valid PalindromeGoEasyO(n)O(1)44.3%
0141Linked List CycleGoEasyO(n)O(1)❤️47.4%
0142Linked List Cycle IIGoMediumO(n)O(1)❤️48.7%
0143Reorder ListGoMedium52.5%
0148Sort ListGoMedium55.1%
0151Reverse Words in a StringGoMedium32.7%
0160Intersection of Two Linked ListsGoEasy54.3%
0167Two Sum II - Input Array Is SortedGoMediumO(n)O(1)60.0%
0189Rotate ArrayGoMedium39.4%
0202Happy NumberGoEasy54.8%
0234Palindrome Linked ListGoEasyO(n)O(1)50.2%
0283Move ZeroesGoEasyO(n)O(1)61.4%
0287Find the Duplicate NumberGoMediumO(n)O(1)❤️59.1%
0344Reverse StringGoEasyO(n)O(1)76.7%
0345Reverse Vowels of a StringGoEasyO(n)O(1)50.1%
0349Intersection of Two ArraysGoEasyO(n)O(n)70.9%
0350Intersection of Two Arrays IIGoEasyO(n)O(n)56.0%
0392Is SubsequenceGoEasy47.6%
0455Assign CookiesGoEasy49.9%
0457Circular Array LoopGoMedium32.6%
0475HeatersGoMedium36.5%
0524Longest Word in Dictionary through DeletingGoMediumO(n)O(1)51.0%
0532K-diff Pairs in an ArrayGoMediumO(n)O(n)41.2%
0541Reverse String IIGoEasy50.5%
0557Reverse Words in a String IIIGoEasy81.9%
0567Permutation in StringGoMediumO(n)O(1)❤️44.3%
0581Shortest Unsorted Continuous SubarrayGoMedium36.4%
0611Valid Triangle NumberGoMedium50.5%
0633Sum of Square NumbersGoMedium34.4%
0653Two Sum IV - Input is a BSTGoEasy61.0%
0658Find K Closest ElementsGoMedium46.8%
0696Count Binary SubstringsGoEasy65.5%
0719Find K-th Smallest Pair DistanceGoHard36.7%
0763Partition LabelsGoMediumO(n)O(1)❤️79.7%
0795Number of Subarrays with Bounded MaximumGoMedium52.8%
0821Shortest Distance to a CharacterGoEasy71.3%
0825Friends Of Appropriate AgesGoMedium46.3%
0826Most Profit Assigning WorkGoMediumO(n log n)O(n)44.9%
0832Flipping an ImageGoEasy80.8%
0838Push DominoesGoMediumO(n)O(n)57.0%
0844Backspace String CompareGoEasyO(n)O(n)48.1%
0845Longest Mountain in ArrayGoMediumO(n)O(1)40.2%
0870Advantage ShuffleGoMedium51.8%
0876Middle of the Linked ListGoEasy75.6%
0881Boats to Save PeopleGoMediumO(n log n)O(1)53.1%
0922Sort Array By Parity IIGoEasy70.7%
09233Sum With MultiplicityGoMediumO(n^2)O(n)45.3%
0925Long Pressed NameGoEasyO(n)O(1)33.1%
0942DI String MatchGoEasy77.3%
0969Pancake SortingGoMedium70.1%
0977Squares of a Sorted ArrayGoEasyO(n)O(1)71.9%
0986Interval List IntersectionsGoMediumO(n)O(1)71.3%
1040Moving Stones Until Consecutive IIGoMedium55.9%
1048Longest String ChainGoMedium59.2%
1089Duplicate ZerosGoEasy51.5%
1332Remove Palindromic SubsequencesGoEasy76.2%
1385Find the Distance Value Between Two ArraysGoEasy66.5%
1679Max Number of K-Sum PairsGoMedium57.3%
1721Swapping Nodes in a Linked ListGoMedium67.2%
1877Minimize Maximum Pair Sum in ArrayGoMedium79.9%

Calendar Jun 28, 2026
Edit Edit this page
Total visits:   You are visitor No.
中文