当前位置:网站首页>The fifth day of June training - double pointer
The fifth day of June training - double pointer
2022-06-12 06:29:00 【Straying into the crowd】
List of articles
Preface
The content of today's algorithm is : Double pointer
One 、 Reverse word prefix
I'll give you a subscript from 0 Starting string word And a character ch . find ch First occurrence of subscript i , reverse word From the subscript 0 Start 、 Until the subscript i end ( Including subscript i ) That part of the character . If word There are no characters in ch , Then no operation is required .
for example , If word = “abcdefd” And ch = “d” , Then you should reverse From the subscript 0 Start 、 Until the subscript 3 end ( Including subscript 3 ). The result string will be “dcbaefd” .
return Result string .
One 、 Ideas :
Double pointer (1) for Under the cycle Again Define a pointer to move , If meet ch, Proceed from i To j Inter character exchange , And then end the loop ;
(2) Can't find ch It ends the cycle ;
Two 、 Source code
class Solution {
public:
string reversePrefix(string word, char ch) {
for(int i=0;i<word.size();i++){
int j=i;
while(j<word.size()&&word[j]!=ch) j++;
if(word[j]==ch){
while(i<j){
int t=word[i];
word[i]=word[j];
word[j]=t;
++i,--j;
}
}
break;
}
return word;
}
};
3、 ... and . Knowledge point
Application of double pointer , The exchange of elements in an array
Two 、 Just reverse the letters
Give you a string s , Reverse the string according to the following rules :
All non English letters remain in their original positions .
All English letters ( Lowercase or uppercase ) Position reversal .
Returns the inverted s .
One 、 Ideas :
Define two pointers , Move from left to right if not Letters move , Scan right to left ; If it is not a letter Move , Then proceed Back and forth exchange , Loop until the pointer collides ;
Two 、 Source code
class Solution {
bool isWord(char a){
return a>='a'&&a<='z'||a>='A'&&a<='Z';
}
void swap(char *a,char *b){
char t=*a;
*a=*b;
*b=t;
}
public:
string reverseOnlyLetters(string s) {
int i=0,j=s.size()-1;
while(i<j){
if(!isWord(s[i])) {
++i;
continue;
}
if(!isWord(s[j])) {
--j;
continue;
}
swap(&s[i],&s[j]);
++i,--j;
}
return s;
}
};
3、 ... and . Knowledge point
Application of double pointer
边栏推荐
- Video summary with long short term memory
- leetcode 278. First wrong version
- Word vector training based on nnlm
- Univariate linear regression model
- Bert use
- Delete the duplicate items in the ordered array -- force deduction question 26 (simple)
- Get the size of the picture
- LeetCode个人题解(剑指offer3-5)3.数组中重复的数字,4.二维数组中的查找,5.替换空格
- 夜神模拟器adb查看log
- Leetcode sword finger offer (Second Edition) complete version of personal questions
猜你喜欢

Redis configuration (III) -- master-slave replication

AI作业ch8
![Set [list] to find out the subscript of repeated elements in the list (display the position of the subscript)](/img/95/67f435646f52646fc6cae8c680d589.jpg)
Set [list] to find out the subscript of repeated elements in the list (display the position of the subscript)

About session Getattribute, getattribute error

Multithreading (4) -- no lock (2) -- Atomic related atomic classes

leetcode 35. Search insert location

LeetCode-419. Battleship on deck

Explanation of sensor flicker/banding phenomenon

Use ms17-010 Eternal Blue vulnerability to infiltrate win7 and establish a permanent back door

Multithreading (4) -- no lock (3) -- longadder source code
随机推荐
SQL注入——联合查询union
Leetcode January 13 daily question 747 At least twice the maximum number of other numbers
LeetCode个人题解(剑指offer3-5)3.数组中重复的数字,4.二维数组中的查找,5.替换空格
Cv2.fillpoly coco annotator segment coordinate conversion to mask image
Get the size of the picture
Multithreading (4) -- no lock (3) -- longadder source code
Multithreading (2) -- pipeline (4) -- Park and unpark
Tips for using the potplayer video player
Process when solving vagrant up_ builder. rb:43:in `join‘: incompatible character encodings: GBK and UTF-8
使用 ms17-010 永恒之蓝漏洞对 win7 进行渗透及建立永久后门
LeetCode-1189. Maximum number of balloons
Opencv_100问_第五章 (21-25)
Redis basic notes
夜神模拟器adb查看log
LeetCode-1716. Calculate the amount deducted from the bank
JS pre parsing
Reentrantlock underlying AQS source code analysis
SQL注入原理即sqli-labs搭建,sql注入简单实战
leetcode 278. First wrong version
Redis data type (VII) -- hyperloglog