第四章 字符串part01
第四章 字符串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-- ;
