第四章 字串part01

晴夜空發表於2024-07-02

344.反轉字串 541. 反轉字串II 卡碼網:54.替換數字

344.反轉字串

題目連結 :

344. 反轉字串 - 力扣(LeetCode)

Code :

class Solution {
public:
void reverseString(vector<char>& s) {

int length_s = s.size();

char char_Cache ;

int length_Half_s = ( length_s / 2 ) ; // 下 取 整

if(length_s % 2 != 0 )
{


}
else
{



}


for(int i = 0 ; i < length_Half_s ; i++ )
{
char_Cache = s[ (length_s - 1 ) - i ] ; // Logic
// Logic 表示 強 的 程式碼 佈置 ,/ 結構 佈置
s[(length_s - 1 ) - i ] = s[i] ;

s[i] = char_Cache ;


}









}
};

541. 反轉字串II

題目連結 :

541. 反轉字串 II - 力扣(LeetCode)

Code :

class Solution {
public:
string reverseStr(string s, int k) {

// 預製 引數

int length_s_From_Count = 0 ;

int i_Work = 0 ;

while(s[i_Work] != '\0')
{
length_s_From_Count++ ;

i_Work ++ ;

}

int k_Multiply_2 = k * 2 ;

int k_Divide_2_Floor = k / 2 ;

int num_Batch_Process_Str_s = length_s_From_Count / k_Multiply_2 ;

int num_CharRemaining = length_s_From_Count % k_Multiply_2 ; // less than ( 2 * k )

char* array_Char_For_Cache ;
array_Char_For_Cache = new char[length_s_From_Count];

// 處理 Part

//cout <<"num_CharRemaining_Divide_2_Floor : " << num_CharRemaining<<endl;

for( int i_Batch = 0 , i_Work2 = 0 ; i_Batch < num_Batch_Process_Str_s ; i_Batch++ ) // i_Work2 = i_Work2 + k // i_Work2 處理 Part 調整 字串 時 的 工作 變數 Batch 首地址 的 構建 i_Batch “ Batch 下標 ”
{
i_Work2 = i_Batch * ( 2 * k) ;
int i_Work3 = (i_Batch + 1 ) * ( 2 * k ) - k - 1 ;
for(int index_SecondFloor = 0 ; index_SecondFloor < k_Divide_2_Floor ;index_SecondFloor ++ )
{
char char_Cache = s[i_Work3];
//字元陣列 處理


// 字元 陣列 的 迴圈 工作 的 後 半 部分 不要 忘了 進行 處理

// / 字串 處理 ? 好像 是 自動 Allocate 新 的 字串 ;

array_Char_For_Cache[i_Work3] = s[i_Work2] ;
array_Char_For_Cache[i_Work2] = s[i_Work3] ;

i_Work2++ ;
i_Work3-- ;


}
if(k % 2 != 0 )
{
array_Char_For_Cache[i_Work2] = s[i_Work2] ;

}

i_Work2 = (i_Batch + 1 ) * ( 2 * k ) - k ;

for(int i = 0 ; i < k ; i++)
{
array_Char_For_Cache[i_Work2] = s[i_Work2] ;

i_Work2 ++ ;

}



}

int num_CharRemaining_Divide_2_Floor = num_CharRemaining / 2 ;

//cout<<" num_CharRemaining_Divide_2_Floor : " << num_CharRemaining_Divide_2_Floor <<endl;

//cout << "111111111111" << endl;

//cout<<"num_CharRemaining : "<<num_CharRemaining<<endl;

if(num_CharRemaining <= k )
{
//cout << "111111111111" << endl;
int i_Work2 = ( (num_Batch_Process_Str_s ) * ( 2 * k ) ) ;
int i_Work3 = ( (num_Batch_Process_Str_s ) * ( 2 * k ) - 1 ) + num_CharRemaining ;
for(int i = 0 ; i < num_CharRemaining_Divide_2_Floor ; i++ )
{
array_Char_For_Cache[i_Work3] = s[i_Work2] ;
array_Char_For_Cache[i_Work2] = s[i_Work3] ;

i_Work2 ++ ;
i_Work3 -- ;


}
if(num_CharRemaining % 2 != 0 || num_CharRemaining == 1)
{
array_Char_For_Cache[i_Work2] = s[i_Work2] ;

}


}
else
{
//cout << "222222222222" << endl;
int i_Work2 = ( (num_Batch_Process_Str_s ) * ( 2 * k ) ) ;
int i_Work3 = ( (num_Batch_Process_Str_s ) * ( 2 * k ) - 1 ) + k ;
//cout<<"i_Work2 : "<<i_Work2<<endl;
//cout<<"i_Work3 : "<<i_Work3<<endl;
for(int i = 0 ; i < k_Divide_2_Floor ; i++ )
{
array_Char_For_Cache[i_Work3] = s[i_Work2] ;
array_Char_For_Cache[i_Work2] = s[i_Work3] ;

i_Work2 ++ ;
i_Work3 -- ;

//cout<<i<<endl;


}
if(k % 2 != 0 )
{
array_Char_For_Cache[i_Work2] = s[i_Work2] ;

}

// 字元 陣列 的 迴圈 工作 的 後 半 部分 不要 忘了 進行 處理

i_Work2 = ( (num_Batch_Process_Str_s ) * ( 2 * k ) ) + k ;

int temp_Diff = num_CharRemaining - k ;

for(int i = 0 ; i < temp_Diff ; i++)
{
array_Char_For_Cache[i_Work2] = s[i_Work2] ;

i_Work2 ++ ;

}


}

//cout<<"333333333333"<<endl;

string str_For_Return = "" ;

//cout<<"length_s_From_Count : "<<length_s_From_Count<<endl;

for(int i = 0 ; i < length_s_From_Count ; i++ )
{
str_For_Return += array_Char_For_Cache[i] ;

//cout<<"array_Char_For_Cache[i] : " << array_Char_For_Cache[i]<<endl;

//cout<<str_For_Return<<endl;

//cout<< 44444444444<<endl;

}

//cout<<str_For_Return<<endl;

//str_For_Return += '\0';

//cout<< 55555555555<<endl;

return str_For_Return ;


// // 動態 處理

// while()


// {
// if( s[i_Work] != '\0' )
// {


// }
// }


}
};

卡碼網:54.替換數字

題目連結 :

54. 替換數字(第八期模擬筆試) (kamacoder.com)

Code :

#include<iostream>
#include<string>
using namespace std ;


int main()
{

string s ,s2 ,s3 ;


//int length_s ;

char * array_Char_Chache ;

std::cin >> s;

//length_s = s.size();

//array_Char_Chache = (char *)malloc(sizeof(char * length_s )) ;

s2 = "";


for(int i = 0 ; s[i] != '\0' ; i++ )
{
if(s[i] >= '0' && s[i] <= '9')
{
s2 += "number" ;



}
else
{
s2 += s[i] ;

}
}


//std::cout << s << std::endl;

std::cout << s2 << std::endl;

return 0 ;




}

相關文章