第四章 字符串part01
344.反转字符串
Code :
class Solution {
public:
void reverseString(vector<char>& s) {
int len = s.size();
int i = 0;
int j = len - 1 ;
while(i < j)
{
char temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
}
}
};
541. 反转字符串II
Code :
class Solution {
public:
string reverseStr(string s, int k) {
int len_s = s.length();
char * str_Return = new char[len_s + 1] ;
int quotient = len_s / ( 2 * k) ;
int remainder = len_s % ( 2 * k) ;
//cout<<"s = "<<s<<endl;
//cout<<"k = "<<k<<endl;
//cout<<"( 2 * k) = "<<( 2 * k)<<endl;
//cout<<"quotient = "<<quotient<<endl;
//cout<<"remainder = "<<remainder<<endl;
int i = 0 ;
int j = 0 ;
for(i = 0 ; i < quotient ; i ++ )
{
for( j = 0 ; j < ( k) ; j++ )
{
//cout<<"str_Return ["<< ((( k) -1) - j) + ( i * ( 2 * k) )<<"] = s["<<j + ( i * ( 2 * k) ) <<"]"<< endl ;
str_Return[ ((( k) -1) - j) + ( i * ( 2 * k) ) ] = s[ j + ( i * ( 2 * k) ) ] ;
// 注意 变量 的 从动 更新
}
for( ; j < (2 * k) ; j++ )
{
str_Return[j + ( i * ( 2 * k) )] = s[j + ( i * ( 2 * k) )];
// 注意 变量 的 从动 更新
}
}
//cout<<"str_Return = "<<str_Return<<endl;
if(remainder < k)
{
i = 0 + (quotient * (2 * k)) ;
j = ( remainder - 1 ) + (quotient * (2 * k));
for( ; i < remainder + (quotient * (2 * k)) ; )
{
str_Return[j] = s[i];
i++ ;
j-- ;
}
str_Return[len_s] = '\0';
}else
if(remainder >= k && remainder < 2*k)
{
i = 0 + (quotient * (2 * k)) ;
j = (k - 1) + (quotient * (2 * k)) ;
for( ; i < k + (quotient * (2 * k)) ; )
{
str_Return[j] = s[i];
i++ ;
j-- ;
}
//str_Return[(k ) + (quotient * (2 * k))] = '\0';
//cout<<"str_Return = "<<str_Return<<endl;
//cout<<"s["<<i<<"] = "<<s[i]<<endl;
for( ; i < remainder + (quotient * (2 * k)) ; )
{
str_Return[i] = s[i];
//cout<<"111111111111111111"<<endl;
i++ ;
}
str_Return[len_s] = '\0';
}
return str_Return ;
}
};
卡码网:54.替换数字
Code :
#include<iostream>
#include<string>
using namespace std;
int main()
{
//char * str_Receive ;
string str_Receive ;
string str_Return = "" ;
cin >> str_Receive ;
//cout<<str_Receive<<endl;
int len_str_Receive = str_Receive.length();
int i = 0 ;
//for( i = 0 ; str_Receive[i] != '\0' ; i++)
for( i = 0 ; i < len_str_Receive ; i++)
{
if(str_Receive[i] == '\0')
{
break;
}
if(str_Receive[i] >= '0' && str_Receive[i] <= '9')
{
str_Return += "number";
}
else
{
str_Return += str_Receive[i] ;
}
}
// 卡码网 的 字符串 输出 检测 居然 不加 '\0'
//str_Return += '\0' ;
cout<<str_Return;
return 0 ;
}
151.翻转字符串里的单词 (使用 栈)
Code :
class Solution {
public:
string reverseWords(string s) {
stack<string> stack_Str ;
string Cache_Str = "" ;
string Str_Receive = "" ;
int i = 0 ;
int len_s = s.length();
//cout<<"len_s = "<<len_s<<endl;
for( i = 0 ; i < len_s ; i++)
{
//if(s[i] != " ") //注意这里要填字符 , //Java 中 判断 字符串 内容 相等,有专门的函数 , 直接判断是判断 地址 是否 相等
if(s[i] != ' ')
{
Cache_Str += s[i] ;
//cout<<"s["<<i<<"]"<<s[i]<<endl;
}
else
{
if(Cache_Str.length() != 0)
{
Cache_Str += "\0" ;
stack_Str.push(Cache_Str);
//cout<<Cache_Str <<endl;
Cache_Str = "" ;
}
}
}
if(Cache_Str.length() != 0)
{
Cache_Str += "\0" ;
stack_Str.push(Cache_Str);
//cout<<Cache_Str <<endl;
Cache_Str = "" ;
}
while(!stack_Str.empty())
{
Str_Receive += stack_Str.top();
stack_Str.pop();
if(!stack_Str.empty())
{
Str_Receive += " " ;
}
}
//Str_Receive += '\0' ;
return Str_Receive ;
}
};
Solution 2 (不使用 栈 , 根据 内容 , 逻辑 进行 分析) :
根据 情况 , 规律 调整 字符串 的 扫描 与 构建 顺序 , 方式
使用 了 " 观察哨 "
Code :
class Solution {
public:
string reverseWords(string s) {
string Cache_Str = "" ;
string Str_Receive = "" ;
int j ;
int len_s = s.length();
int Find_First_Word = 1;
//cout<<"len_s = "<<len_s<<endl;
for( j = len_s - 1 ; j >= 0 ; j--)
{
//if(s[i] != " ") //注意这里要填字符 , //Java 中 判断 字符串 内容 相等,有专门的函数 , 直接判断是判断 地址 是否 相等
if(s[j] != ' ')
{
Cache_Str = s[j] + Cache_Str ;
//cout<<"s["<<j<<"]"<<s[j]<<endl;
}
else
{
if(Cache_Str.length() != 0)
{
if(Find_First_Word == 0) // 正常 顺序 时 If 不是 第1个 单词,就 前置 加 空格
{ //倒序 时 , 最后 一个 单词 也 就是 最先被读取的 前边 不加 空格
Cache_Str = " " + Cache_Str ;
}
Cache_Str += "\0" ;
Str_Receive += Cache_Str ;
Find_First_Word = 0;
//cout<<Cache_Str <<endl;
Cache_Str = "" ;
}
}
}
if(Cache_Str.length() != 0)
{
if(Find_First_Word == 0)
{
Cache_Str = " " + Cache_Str ;
}
Cache_Str += "\0" ;
Str_Receive += Cache_Str ;
//cout<<Cache_Str <<endl;
Cache_Str = "" ;
}
//Str_Receive += '\0' ;
return Str_Receive ;
}
};
卡码网:55.右旋转字符串
Code :
#include<iostream>
#include<string>
using namespace std;
int main()
{
int len_Turn ;
string str_In ;
int len_str_In;
string str_Return = "" ;
cin >> len_Turn ;
cin >> str_In ;
len_str_In = str_In.length();
// 下标 引导
int i = len_str_In - len_Turn ;
for( ; i < len_str_In ; i++ )
{
str_Return += str_In[i] ;
}
for( i = 0 ; i < len_str_In - len_Turn ; i++ )
{
str_Return += str_In[i] ;
}
cout<<str_Return;
return 0 ;
}