344. 反转字符串
题目链接:https://leetcode.cn/problems/reverse-string/description/
文章讲解:https://programmercarl.com/0344.%E5%8F%8D%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2.html
视频讲解:https://www.bilibili.com/video/BV1fV4y17748/
class Solution {
public :
void reverseString ( vector< char> & s ) {
int len = s. size ( ) ;
for ( int i = 0 , j = len - 1 ; i < len / 2 ; i++ , j-- )
{
swap ( s[ i] , s[ j] ) ;
}
return ;
}
} ;
541. 反转字符串 II
题目链接:https://leetcode.cn/problems/reverse-string-ii/description/
文章讲解:https://programmercarl.com/0541.%E5%8F%8D%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2II.html
视频讲解:https://www.bilibili.com/video/BV1dT411j7NN/
class Solution {
public :
string reverseStr ( string s, int k ) {
for ( int i = 0 ; i < s. size ( ) ; i+= 2 * k)
{
if ( i + k <= s. size ( ) )
{
reverse ( s. begin ( ) + i, s. begin ( ) + i+ k) ;
}
else
{
reverse ( s. begin ( ) + i, s. end ( ) ) ;
}
}
return s;
}
} ;
卡码网:54.替换数字
题目链接:https://kamacoder.com/problempage.php?pid=1064
文章讲解:https://programmercarl.com/kama54.%E6%9B%BF%E6%8D%A2%E6%95%B0%E5%AD%97.html#%E6%80%9D%E8%B7%AF
视频讲解:
#include< iostream>
using namespace std;
int main ( ) {
string s;
while ( cin >> s) {
int count = 0 ;
int sOldSize = s. size ( ) ;
for ( int i = 0 ; i < s. size ( ) ; i++ ) {
if ( s[ i] >= '0' && s[ i] <= '9' ) {
count++ ;
}
}
s. resize ( s. size ( ) + count * 5 ) ;
int sNewSize = s. size ( ) ;
for ( int i = sNewSize - 1 , j = sOldSize - 1 ; j < i; i-- , j-- ) {
if ( s[ j] > '9' || s[ j] < '0' ) {
s[ i] = s[ j] ;
} else {
s[ i] = 'r' ;
s[ i - 1 ] = 'e' ;
s[ i - 2 ] = 'b' ;
s[ i - 3 ] = 'm' ;
s[ i - 4 ] = 'u' ;
s[ i - 5 ] = 'n' ;
i -= 5 ;
}
}
cout << s << endl;
}
}
这道理留着以后再来看。
151. 反转字符串中的单词
题目链接:https://leetcode.cn/problems/reverse-words-in-a-string/description/
文章讲解:https://programmercarl.com/0151.%E7%BF%BB%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2%E9%87%8C%E7%9A%84%E5%8D%95%E8%AF%8D.html#%E6%80%9D%E8%B7%AF
视频讲解:https://www.bilibili.com/video/BV1fV4y17748/
class Solution {
public :
void reverse ( string& s, int start, int end ) {
for ( int i = start, j = end; i < j; i++ , j-- ) {
swap ( s[ i] , s[ j] ) ;
}
}
void removeExtraSpaces ( string& s ) {
int slow = 0 ;
for ( int i = 0 ; i < s. size ( ) ; ++ i) {
if ( s[ i] != ' ' ) {
if ( slow != 0 ) s[ slow++ ] = ' ' ;
while ( i < s. size ( ) && s[ i] != ' ' ) {
s[ slow++ ] = s[ i++ ] ;
}
}
}
s. resize ( slow) ;
}
string reverseWords ( string s ) {
removeExtraSpaces ( s) ;
reverse ( s, 0 , s. size ( ) - 1 ) ;
int start = 0 ;
for ( int i = 0 ; i <= s. size ( ) ; ++ i) {
if ( i == s. size ( ) || s[ i] == ' ' ) {
reverse ( s, start, i - 1 ) ;
start = i + 1 ;
}
}
return s;
}
} ;
右旋字符串
题目链接:https://programmercarl.com/kama55.%E5%8F%B3%E6%97%8B%E5%AD%97%E7%AC%A6%E4%B8%B2.html
文章讲解:https://programmercarl.com/0344.%E5%8F%8D%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2.html
视频讲解:https://www.bilibili.com/video/BV1fV4y17748/