当前位置:网站首页>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
边栏推荐
- Book classification based on Naive Bayes
- . Net core - pass Net core will Net to cross platform
- Unity surface shader with template buffer
- LeetCode-884. Unusual words in two sentences
- Multithreading Foundation (XI) -- prevent CPU from occupying 100%
- Redis data type (VII) -- hyperloglog
- . Net core and Net framework comparison
- Leetcode personal question solution (Sword finger offer3-5) 3 Duplicate number in array, 4 Find in 2D array, 5 Replace spaces
- Opencv_ 100 questions_ Chapter V (21-25)
- leetcode 704. Binary search
猜你喜欢
随机推荐
LeetCode-1405. Longest happy string
PHP 读写 COOKIE
PHP development environment construction and database addition, deletion, modification and query
Summary of some problems in sensor bring up
2021 RoboCom 世界机器人开发者大赛-本科组(初赛)
LeetCode-1576. Replace all question marks
JS pre parsing
SQL 注入读写文件
Get the size of the picture
platform driver
C # converts the hexadecimal code form of text to text (ASCII)
Cause analysis of motion blur / smear caused by camera shooting moving objects
Video summary with long short term memory
Multithreading (2) -- pipeline (4) -- Park and unpark
Computer composition and design work06 - based on MIPS
六月集训 第七日 ——哈希表
Overview of camera image quality
Redis application (I) -- distributed lock
Delete the duplicate items in the ordered array -- force deduction question 26 (simple)
Reentrantlock underlying AQS source code analysis









