第七章 回溯演算法part01

晴夜空發表於2024-07-13

第七章 回溯演算法part01

77. 組合 216.組合總和III 17.電話號碼的字母組合

77. 組合

題目連結 :

77. 組合 - 力扣(LeetCode)

Code ( 用 遞迴 模擬 迴圈 ) :

class Solution {
public:
vector<vector<int>> combine(int n, int k) {

vector<int> vec_Temp_Cache_For_RecordNum ;

vector<vector<int>> vec_2_Dimention_For_Receive_And_Return ;

work_Recursino_For_forLoop( 1 , 1 , n , k , vec_Temp_Cache_For_RecordNum , vec_2_Dimention_For_Receive_And_Return ) ;


return vec_2_Dimention_For_Receive_And_Return ;



}




void work_Recursino_For_forLoop( int i_Work_In , int i_Layer , int n , int k , vector<int> & vec_Temp_Cache_For_RecordNum , vector<vector<int>> & vec_2_Dimention_For_Receive_And_Return )
{

if(i_Layer != k ) // 邏輯 性 / 安全 性 / 效率
{
for( int i = i_Work_In; i <= (( n - k ) + i_Layer ) ; i++ )
{
//vec_Temp_Cache_For_RecordNum[ (i_Layer - 1 ) ] = i ;

vec_Temp_Cache_For_RecordNum.push_back(i) ;


work_Recursino_For_forLoop( (i + 1 ) , ( i_Layer + 1 ) , n , k , vec_Temp_Cache_For_RecordNum , vec_2_Dimention_For_Receive_And_Return ) ;

vec_Temp_Cache_For_RecordNum.pop_back() ;


}




}
else
{
for( int i = i_Work_In; i <= (( n - k ) + i_Layer ) ; i++ )
{

//vec_Temp_Cache_For_RecordNum[ (i_Layer - 1 ) ] = i ;

vec_Temp_Cache_For_RecordNum.push_back(i) ;

vec_2_Dimention_For_Receive_And_Return.push_back(vec_Temp_Cache_For_RecordNum ) ;

//work_Recursino_For_forLoop( (i + 1 ) , ( i_Layer + 1 ) , n , k )

vec_Temp_Cache_For_RecordNum.pop_back() ;


}



}




}
};

216.組合總和III

題目連結 :

216. 組合總和 III - 力扣(LeetCode)

Code ( 遞迴 遍歷 , 回溯 , 不斷 地 更新 Cache 向量 中 的 值 , Cache Sum ) :

class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n) {

int target = n ;


vector<int> vec_Temp_Cache_For_NumSequence ;

vector<vector<int>> vec_2_Dimention_For_Receive_And_Return ;

int num_Cache_Sum = 0 ;



// 嘗試 遞迴 遍歷



fuction_Work_Recursion_For_forLoop(1 , 1 , k , target , vec_Temp_Cache_For_NumSequence , vec_2_Dimention_For_Receive_And_Return , num_Cache_Sum ) ;


return vec_2_Dimention_For_Receive_And_Return ;





}




void fuction_Work_Recursion_For_forLoop( int i_Work_In , int i_Layer , int k , int target , vector<int> & vec_Temp_Cache_For_NumSequence , vector<vector<int>> & vec_2_Dimention_For_Receive_And_Return , int & num_Cache_Sum )
{ // “ 省 記憶體 ” , Question : 會 省 暫存器 嗎 ? // 程式碼 會不會 自動 地 進行 相關 最佳化

if( i_Layer != k )
{
for( int i = i_Work_In ; i <= (( 9 - k ) + i_Layer ) ; i++ )
{

num_Cache_Sum += i ;

vec_Temp_Cache_For_NumSequence.push_back(i) ;

//

fuction_Work_Recursion_For_forLoop( ( i + 1 ) , ( i_Layer + 1 ) , k , target , vec_Temp_Cache_For_NumSequence , vec_2_Dimention_For_Receive_And_Return , num_Cache_Sum ) ;


num_Cache_Sum -= i ;

vec_Temp_Cache_For_NumSequence.pop_back() ;



}



}
else
{
for( int i = i_Work_In ; i <= (( 9 - k ) + i_Layer ) ; i++ )
{

num_Cache_Sum += i ;

vec_Temp_Cache_For_NumSequence.push_back(i) ;

//

if(num_Cache_Sum == target )
{

vec_2_Dimention_For_Receive_And_Return.push_back(vec_Temp_Cache_For_NumSequence ) ;


}



num_Cache_Sum -= i ;

vec_Temp_Cache_For_NumSequence.pop_back() ;






}



}





}


};

17.電話號碼的字母組合

題目地址 :

17. 電話號碼的字母組合 - 力扣(LeetCode)

Code ( 版本 1 ( Cache 字串 引用 + 維護 ) ) :

class Solution {
public:
vector<string> letterCombinations(string digits) {

vector<vector<string>> vec_Resource_Num_Map_Letter = { { "a" , "b" , "c" }, { "d" , "e" , "f" } , {"g","h","i"} , {"j","k","l"} , {"m","n","o"} , {"p" , "q" , "r" , "s" } , { "t" , "u" , "v"} , { "w" , "x" , "y" , "z" } } ;

//vec_Resource_Num_Map_Letter

string str_Cache = "" ;

vector<string> vec_Str_For_Receive_And_Return ;



if( digits == "")
{

return vec_Str_For_Receive_And_Return ;


}


fuction_Work_Recursion_For_forLoop( 0 , str_Cache , vec_Str_For_Receive_And_Return , vec_Resource_Num_Map_Letter , digits ) ;



return vec_Str_For_Receive_And_Return ;

}


void fuction_Work_Recursion_For_forLoop(int i_Layer , string & str_Cache , vector<string> & vec_Str_For_Receive_And_Return , vector<vector<string>> & vec_Resource_Num_Map_Letter , string digits )
{
char char_Cache ;


if( digits[i_Layer + 1] != '\0' )
{
char_Cache = digits[i_Layer ] ;

int num_Temp_Cache = int ((char_Cache - 0x30 ) - 2 ) ;


for(int i = 0 ; i < vec_Resource_Num_Map_Letter[num_Temp_Cache].size() ; i++ )
{
str_Cache += vec_Resource_Num_Map_Letter[num_Temp_Cache][i] ;


//

fuction_Work_Recursion_For_forLoop( (i_Layer + 1 ) , str_Cache , vec_Str_For_Receive_And_Return , vec_Resource_Num_Map_Letter , digits ) ;


str_Cache = str_Cache.substr(0 , i_Layer ) ;


}



}
else
{
char_Cache = digits[i_Layer ] ;

int num_Temp_Cache = int ((char_Cache - 0x30 ) - 2 ) ;


for(int i = 0 ; i < vec_Resource_Num_Map_Letter[num_Temp_Cache].size() ; i++ )
{
str_Cache += vec_Resource_Num_Map_Letter[num_Temp_Cache][i] ;


//
vec_Str_For_Receive_And_Return.push_back(str_Cache ) ;


str_Cache = str_Cache.substr(0 , i_Layer ) ;


}





}





}




};

Code ( 版本 2 ( Cache 字串 傳值) ) :

class Solution {
public:
vector<string> letterCombinations(string digits) {

vector<vector<string>> vec_Resource_Num_Map_Letter = { { "a" , "b" , "c" }, { "d" , "e" , "f" } , {"g","h","i"} , {"j","k","l"} , {"m","n","o"} , {"p" , "q" , "r" , "s" } , { "t" , "u" , "v"} , { "w" , "x" , "y" , "z" } } ;

//vec_Resource_Num_Map_Letter

string str_Cache = "" ;

vector<string> vec_Str_For_Receive_And_Return ;



if( digits == "")
{

return vec_Str_For_Receive_And_Return ;


}


fuction_Work_Recursion_For_forLoop( 0 , str_Cache , vec_Str_For_Receive_And_Return , vec_Resource_Num_Map_Letter , digits ) ;



return vec_Str_For_Receive_And_Return ;

}


void fuction_Work_Recursion_For_forLoop(int i_Layer , string str_Cache , vector<string> & vec_Str_For_Receive_And_Return , vector<vector<string>> & vec_Resource_Num_Map_Letter , string digits )
{
char char_Cache ;


if( digits[i_Layer + 1] != '\0' )
{
char_Cache = digits[i_Layer ] ;

int num_Temp_Cache = int ((char_Cache - 0x30 ) - 2 ) ;


for(int i = 0 ; i < vec_Resource_Num_Map_Letter[num_Temp_Cache].size() ; i++ )
{
//str_Cache += vec_Resource_Num_Map_Letter[num_Temp_Cache][i] ;


//

fuction_Work_Recursion_For_forLoop( (i_Layer + 1 ) , (str_Cache + vec_Resource_Num_Map_Letter[num_Temp_Cache][i] ) , vec_Str_For_Receive_And_Return , vec_Resource_Num_Map_Letter , digits ) ;


//str_Cache = str_Cache.substr(0 , i_Layer ) ;


}



}
else
{
char_Cache = digits[i_Layer ] ;

int num_Temp_Cache = int ((char_Cache - 0x30 ) - 2 ) ;


for(int i = 0 ; i < vec_Resource_Num_Map_Letter[num_Temp_Cache].size() ; i++ )
{
//str_Cache += vec_Resource_Num_Map_Letter[num_Temp_Cache][i] ;


//
vec_Str_For_Receive_And_Return.push_back(str_Cache + vec_Resource_Num_Map_Letter[num_Temp_Cache][i] ) ;


//str_Cache = str_Cache.substr(0 , i_Layer ) ;


}





}





}




};

相關文章