当前位置:网站首页>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
边栏推荐
- LeetCode-1587. Bank account summary II
- AI作业ch8
- Computer composition and design work05 ——fifth verson
- Unity custom translucent surface material shader
- Are you still using like+% for MySQL fuzzy query?
- Android studio mobile development creates a new database and obtains picture and text data from the database to display on the listview list
- PHP read / write cookie
- Nocturnal simulator ADB view log
- LeetCode-1629. Key with the longest key duration
- QT--实现TCP通信
猜你喜欢

PHP 读写 COOKIE

Multithreading mode (I) -- protective pause and join source code

LeetCode-419. Battleship on deck

SQL 注入读写文件

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

Solution: content type 'application/x-www-form-urlencoded; charset=UTF-8‘ not supported

分段贝塞尔曲线
![[reinstall system] 01 system startup USB flash disk production](/img/0d/9b3d4b8e286a75f8b58e35d02f261b.jpg)
[reinstall system] 01 system startup USB flash disk production

夜神模拟器adb查看log

夜神模擬器adb查看log
随机推荐
Zip and Items() difference
MNIST handwritten data recognition by RNN
Bert use
Remap function of C different interval mapping
AI作业ch8
platform driver
Word vector training based on nnlm
集合判断存在交集
English语法_副词_有无ly,意义不同
N-degree Bessel curve
六月集训 第一日——数组
SQL injection - blind injection
LeetCode-1078. Bigram participle
AI operation ch8
LeetCode-1716. Calculate the amount deducted from the bank
SQL injection - Union query
Solution: unsatisfieddependencyexception: error creating bean with name 'authaspect':
LeetCode-1576. Replace all question marks
Redis configuration (IV) -- cluster
Unreal Engine learning notes