当前位置:网站首页>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
边栏推荐
- What are the password requirements for waiting insurance 2.0? What are the legal bases?
- Another option for ERP upgrade, MES system
- RT thread bidirectional linked list (learning notes)
- Array method
- 【Matlab红绿灯识别】红绿灯识别【含GUI源码 1908期】
- TACo:一种关于文字识别的数据增强技术
- 请问下,mysqlcdc设置多并行度的话,增量数据是不是会重复?
- What is the level 3 password complexity of ISO? How often is it replaced?
- Bitlock recovery occurs in win 10, and the blue screen error code is 0x1600007e
- How do I get the STW (pause) time of a GC (garbage collector)?
猜你喜欢

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

测试/开发程序员真的是青春饭吗?世界是公平的,咱们都凭实力说话......

Multithreading and high concurrency IV: varhandle, strong weak virtual reference and ThreadLocal

10: 00 interview, came out at 10:02, the question is really too

leetcode - 329. Longest increasing path in matrix

Little knowledge about function templates --

抖音实战~取关博主

Secouer le son et se battre ~ prêter attention au blogueur

Google Earth Engine(GEE)——全球洪水数据库 v1 (2000-2018年)

The growth summer challenge is coming | learn and create two major tracks, and start the tutor registration!
随机推荐
易周金融 | Q1手机银行活跃用户规模6.5亿;理财子公司布局新兴领域
如何遍历collections.OrderedDict,服了又忘记items
What is the level 3 password complexity of ISO? How often is it replaced?
[applet] solution document using font awesome Font Icon (picture and text)
云厂商为什么都在冲这个KPI?
Severe tire damage: the first rock band in the world to broadcast live on the Internet
视频直播系统源码,倒计时显示,商品秒杀倒计时
灵活的IP网络测试工具——— X-Launch
Necessary skills for test and development: actual combat of security test vulnerability shooting range
27年,微软IE结束了!
Simple factory mode
JS逆向之巨量星图sign签名
抖音實戰~關注博主
Has anyone ever used CDC to synchronize to MySQL with a deadlock?
Ppt production tips
Array method
一文详解|增长那些事儿
求一个能判断表中数据,只填充不覆盖的sql
华为9年经验的软件测试总监工作感悟—写给还在迷茫的朋友
Multithreading and high concurrency V: detailed explanation of wait queue, executor and thread pool (key)


