当前位置:网站首页>Play with double pointer
Play with double pointer
2022-06-28 04:41:00 【rockvine】
List of articles
- One 、 Algorithm interpretation
- Two 、 Sum of two numbers
- 3、 ... and 、 Array merge
- Four 、 Speed pointer
- 5、 ... and 、 The sliding window
- 6、 ... and 、 practice
One 、 Algorithm interpretation
Double pointers are mainly used to traverse arrays , Two pointers point to different elements , In order to accomplish the task together . It can also be extended to multiple pointers of multiple arrays .
If two pointers point to the same array , Traversal direction is the same and will not intersect , It is also called sliding window ( The area surrounded by two pointers is the current window ), It is often used for interval search .
If two pointers point to the same array , But the traversal direction is opposite , It can be used to search , The array to be searched is often ordered .
Two 、 Sum of two numbers
2.1、 Sum of two numbers II - Input an ordered array
2.1.1、 Title Description
167. Sum of two numbers II - Input an ordered array
I'll give you a subscript from 1 The starting array of integersnumbers, The array has been pressed Non decreasing order , Please find out from the array that the sum satisfying the addition is equal to the target numbertargetTwo numbers of . Let these two numbers benumbers[index1]andnumbers[index2], be1 <= index1 < index2 <= numbers.length.
In length 2 Array of integers for[index1, index2]Returns the subscript of these two integers in the form ofindex1andindex2.
You can assume that each input Only corresponding to the only answer , And you Can not be Reuse the same elements .
The solution you design must use only constant level extra space .
2.1.2、 I / O example
Example 1:
Input : numbers = [2, 7, 11, 15], target = 9
Output : [1, 2]
explain : 2 And 7 The sum is equal to the number of targets 9 . therefore index1 = 1, index2 = 2 . return [1, 2] .
Example 2:
Input : numbers = [2, 3, 4], target = 6
Output : [1, 3]
explain : 2 And 4 The sum is equal to the number of targets 6 . therefore index1 = 1, index2 = 3 . return [1, 3] .
Example 3:
Input : numbers = [-1, 0], target = -1
Output : [1, 2]
explain : -1 And 0 The sum is equal to the number of targets -1 . therefore index1 = 1, index2 = 2 . return [1, 2] .
2.1.3、 Answer key
3、 ... and 、 Array merge
3.1、 Merge two ordered arrays
3.1.1、 Title Description
88. Merge two ordered arrays
Here are two buttons Non decreasing order Array of arranged integers nums1 andnums2, There are two other integersmandn, respectivelynums1andnums2The number of elements in .
Would you please Merge nums2 To nums1 in , Make the merged array press Non decreasing order array .
Be careful : Final , The merged array should not be returned by the function , It's stored in an arraynums1in . In response to this situation ,nums1The initial length of ism + n, The topmElements represent the elements that should be merged , afternElements are0, It should be ignored .nums2The length of isn.
3.1.2、 I / O example
Example 1:
Input : nums1 = [1, 2, 3, 0, 0, 0], m = 3, nums2 = [2, 5, 6], n = 3
Output : [1, 2, 2, 3, 5, 6]
explain : Need merger [1, 2, 3] and [2, 5, 6] . The combined result is [1, 2, 2, 3, 5, 6] ,
In which, bold italics indicates nums1 The elements in .
Example 2:
Input : nums1 = [1], m = 1, nums2 = [], n = 0
Output : [1]
explain : Need merger [1] and [] .
The combined result is [1] .
Example 3:
Input : nums1 = [0], m = 0, nums2 = [1], n = 1
Output : [1]
explain : The array to be merged is [] and [1] .
The combined result is [1] .
Be careful , because m = 0 , therefore nums1 No elements in .nums1 The only remaining 0 Just to ensure that the merged results can be successfully stored in nums1 in .
3.1.3、 Answer key
Four 、 Speed pointer
4.1、 Circular list II
4.1.1、 Title Description
142. Circular list II
Given the head node of a linked listhead, Return to the first node of the link where the list begins to enter . If the list has no links , Then return tonull.
If there is a node in the linked list , It can be done by continuously trackingnextThe pointer reaches again , Then there is a ring in the linked list . To represent a ring in a given list , The evaluation system uses an integerposTo indicate where the end of the list is connected to the list ( Index from 0 Start ). Ifposyes-1, There are no links in the list . Be careful :pos Not passed as an argument , Just to identify the actual situation of the linked list .
No modification allowed Linked list .
4.1.2、 I / O example
Example 1:
Input : head = [3, 2, 0, -4], pos = 1
Output : The return index is 1 The linked list node of
explain : There is a link in the list , Its tail is connected to the second node .
Example 2:
Input : head = [1, 2], pos = 0
Output : The return index is 0 The linked list node of
explain : There is a link in the list , Its tail is connected to the first node .
Example 3:
Input : head = [1], pos = -1
Output : return null
explain : There are no links in the list .
4.1.3、 Answer key
5、 ... and 、 The sliding window
5.1、 Minimum cover substring
5.1.1、 Title Description
76. Minimum cover substring
Give you a strings、 A stringt. returnsCovered bytThe smallest string of all characters . IfsThere is no coverage intSubstring of all characters , Returns an empty string"".
Be careful :
- about
tDuplicate characters in , The number of characters in the substring we are looking for must not be less thantThe number of characters in the .- If
sThere is such a substring in , We guarantee that it is the only answer .
5.1.2、 I / O example
Example 1:
Input : s = “ADOBECODEBANC”, t = “ABC”
Output : “BANC”
Example 2:
Input : s = “a”, t = “a”
Output : “a”
Example 3:
Input : s = “a”, t = “aa”
Output : “”
explain : t Two characters in ‘a’ Shall be included in s In the string of ,
Therefore, there is no qualified substring , Returns an empty string .
5.1.3、 Answer key
6、 ... and 、 practice
6.1、 Basic difficulty
6.1.1、 Sum of squares
6.1.1.1、 Title Description
633. Sum of squares
Given a nonnegative integerc, You have to decide if there are two integersaandb, bringa2 + b2 = c.
6.1.1.2、 I / O example
Example 1:
Input : c = 5
Output : true
explain : 1 * 1 + 2 * 2 = 5
Example 2:
Input : c = 3
Output : false
6.1.1.3、 Answer key
6.1.2、 Verify palindrome string Ⅱ
6.1.2.1、 Title Description
680. Verify palindrome string Ⅱ
Given a non empty strings, most Delete a character . Determine whether it can be a palindrome string .
6.1.2.2、 I / O example
Example 1:
Input : s = “aba”
Output : true
Example 2:
Input : s = “abca”
Output : true
explain : You can delete c character .
Example 3:
Input : s = “abc”
Output : false
6.1.2.3、 Answer key
6.1.3、 Delete the longest letters from the dictionary
6.1.3.1、 Title Description
524. Delete the longest letters from the dictionary
Give you a stringsAnd an array of stringsdictionary, Find out and return todictionaryThe longest string in , The string can be deletedsSome characters in get .
If there is more than one answer , Returns the string with the longest length and the smallest alphabetical order . If the answer doesn't exist , Returns an empty string .
6.1.3.2、 I / O example
Example 1:
Input : s = “abpcplea”, dictionary = [“ale”, “apple”, “monkey”, “plea”]
Output : “apple”
Example 2:
Input : s = “abpcplea”, dictionary = [“a”, “b”, “c”]
Output : “a”
6.1.3.3、 Answer key
6.2、 Advanced difficulty
6.2.1、 At most K Longest substring of different characters
6.2.1.1、 Title Description
340. At most K Longest substring of different characters
Given a strings, find at most contain k Longest substring of different charactersT.
6.2.1.2、 I / O example
Example 1:
Input : s = “eceba”, k = 2
Output : 3
explain : be T by “ece”, So the length is 3.
Example 2:
Input : s = “aa”, k = 1
Output : 2
explain : be T by “aa”, So the length is 2.
6.2.1.3、 Answer key
边栏推荐
- 测试/开发程序员真的是青春饭吗?世界是公平的,咱们都凭实力说话......
- Games104 operation 2-colorgrading
- RT thread bidirectional linked list (learning notes)
- xml&nbsp; File read / write
- Multithreading and high concurrency II: detailed introduction to volatile and CAS
- 互联网的发展促进了无界零售、数字零售、即时零售等一系列新模式的出现
- C语言全局变量(c文件和h文件中的全局变量、静态全局变量)使用注意事项
- 灵活的IP网络测试工具——— X-Launch
- 简单工厂模式
- JS逆向之巨量星图sign签名
猜你喜欢

10:00面试,10:02就出来了 ,问的实在是太...

leetcode - 329. Longest increasing path in matrix

有关函数模板的那些小知识-.-

MySQL gets the current date of the year

Go language -select statement

Severe tire damage: the first rock band in the world to broadcast live on the Internet

JVM I: introduction to JVM and understanding of class files

Games104 operation 2-colorgrading

2022-06-27:给出一个长度为n的01串,现在请你找到两个区间, 使得这两个区间中,1的个数相等,0的个数也相等, 这两个区间可以相交,但是不可以完全重叠,即两个区间的左右端点不可以完全一样。

简单工厂模式
随机推荐
Problems with cat and dog queues
Source code of live video system, countdown display, countdown of commodity spike
Lamaba expression learning and common functional interfaces
A queue of two stacks
Learning about DC-DC step-down chip of sy8120i (12V reduced to 3.3V)
To quickly download JDK, in addition to the official Oracle download, is there a download address for the latest version available in China
After launching the MES system, these changes have taken place in the enterprise
【Matlab BP回归预测】GA优化BP回归预测(含优化前的对比)【含源码 1901期】
[applet] solution document using font awesome Font Icon (picture and text)
OracleData安装问题
Find an SQL that can judge the data in the table and only fill in the SQL that is not overwritten
10:00面试,10:02就出来了 ,问的实在是太...
Reading notes of top performance version 2 (II) -- Performance observation tool
Matlab exercises -- routine operation of matrix
恭喜我自己,公众号粉丝破万
Tiktok practice ~ pay attention to bloggers
CUPTI error: CUPTI could not be loaded or symbol could not be found.
Annual comprehensive analysis of China's audio market in 2022
Resolve cross domain
Multithreading and high concurrency III: AQS underlying source code analysis and implementation classes


