文章目录
代码随想录在B站的视频讲得比AcWing好 模板题1:排列数字 模板题2:n皇后
LeetCode 46. 全排列 LeetCode 47. 全排列 II (重复元素) LeetCode 39. 组合总和 LeetCode 77. 组合 LeetCode 216. 组合总和 III LeetCode 17. 电话号码的字母组合
代码随想录在B站的视频讲得比AcWing好
模板题1:排列数字
# include <iostream>
using namespace std;
const int N = 7 ;
int n;
int path[ N] ;
bool st[ N] ;
void dfs ( int u) {
if ( u == n) {
for ( int i = 0 ; i < n; i ++ ) {
printf ( "%d " , path[ i] ) ;
}
puts ( "" ) ;
return ;
}
for ( int i = 1 ; i <= n; i ++ ) {
if ( ! st[ i] ) {
path[ u] = i;
st[ i] = true ;
dfs ( u + 1 ) ;
st[ i] = false ;
}
}
}
int main ( ) {
cin >> n;
dfs ( 0 ) ;
return 0 ;
}
模板题2:n皇后
方法一
# include <iostream>
# include <cstdio>
# include <algorithm>
using namespace std;
const int N = 10 ;
char g[ N] [ N] ;
int n;
bool col[ N] , dg[ N] , udg[ N] ;
void dfs ( int u)
{
if ( u == n)
{
for ( int i = 0 ; i < n; i++ ) puts ( g[ i] ) ;
puts ( "" ) ;
return ;
}
for ( int i = 0 ; i < n; i++ )
{
if ( ! col[ i] && ! dg[ u+ i] && ! udg[ i- u+ n] )
{
g[ u] [ i] = 'Q' ;
col[ i] = dg[ u+ i] = udg[ i- u+ n] = true ;
dfs ( u+ 1 ) ;
g[ u] [ i] = '.' ;
col[ i] = dg[ u+ i] = udg[ i- u+ n] = false ;
}
}
}
int main ( )
{
scanf ( "%d" , & n) ;
for ( int i = 0 ; i < n; i++ )
for ( int j = 0 ; j < n; j++ )
g[ i] [ j] = '.' ;
dfs ( 0 ) ;
return 0 ;
}
方法二
# include <iostream>
using namespace std;
const int N = 20 ;
int n;
char g[ N] [ N] ;
bool row[ N] , col[ N] , dg[ N] , udg[ N] ;
void dfs ( int x, int y, int s) {
if ( y == n) {
y = 0 ;
x++ ;
}
if ( x == n) {
if ( s == n) {
for ( int i = 0 ; i < n; i ++ ) {
puts ( g[ i] ) ;
}
puts ( "" ) ;
}
return ;
}
dfs ( x, y + 1 , s) ;
if ( ! row[ x] && ! col[ y] && ! dg[ x + y] && ! udg[ x - y + n] ) {
g[ x] [ y] = 'Q' ;
row[ x] = col[ y] = dg[ x + y] = udg[ x - y + n] = true ;
dfs ( x , y + 1 , s + 1 ) ;
row[ x] = col[ y] = dg[ x + y] = udg[ x - y + n] = false ;
g[ x] [ y] = '.' ;
}
}
int main ( ) {
cin >> n;
for ( int i = 0 ; i < n; i ++ ) {
for ( int j = 0 ; j < n; j ++ ) {
g[ i] [ j] = '.' ;
}
}
dfs ( 0 , 0 , 0 ) ;
return 0 ;
}
LeetCode 46. 全排列
class Solution {
public :
vector< vector< int >> result;
vector< int > path;
void backtracking ( vector< int > & nums, vector< bool > & used) {
if ( path. size ( ) == nums. size ( ) ) {
result. push_back ( path) ;
return ;
}
for ( int i = 0 ; i < nums. size ( ) ; i++ ) {
if ( used[ i] == true ) continue ;
used[ i] = true ;
path. push_back ( nums[ i] ) ;
backtracking ( nums, used) ;
path. pop_back ( ) ;
used[ i] = false ;
}
}
vector< vector< int >> permute ( vector< int > & nums) {
result. clear ( ) ;
path. clear ( ) ;
vector< bool > used ( nums. size ( ) , false ) ;
backtracking ( nums, used) ;
return result;
}
} ;
作者:carlsun- 2
链接:https:
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
LeetCode 47. 全排列 II (重复元素)
class Solution {
private :
vector< vector< int >> result;
vector< int > path;
void backtracking ( vector< int > & candidates, int target, int sum, int startIndex) {
if ( sum == target) {
result. push_back ( path) ;
return ;
}
for ( int i = startIndex; i < candidates. size ( ) && sum + candidates[ i] <= target; i++ ) {
sum += candidates[ i] ;
path. push_back ( candidates[ i] ) ;
backtracking ( candidates, target, sum, i) ;
sum -= candidates[ i] ;
path. pop_back ( ) ;
}
}
public :
vector< vector< int >> combinationSum ( vector< int > & candidates, int target) {
result. clear ( ) ;
path. clear ( ) ;
sort ( candidates. begin ( ) , candidates. end ( ) ) ;
backtracking ( candidates, target, 0 , 0 ) ;
return result;
}
} ;
作者:carlsun- 2
链接:https:
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
LeetCode 39. 组合总和
class Solution {
private :
vector< vector< int >> result;
vector< int > path;
void backtracking ( vector< int > & candidates, int target, int sum, int startIndex) {
if ( sum == target) {
result. push_back ( path) ;
return ;
}
for ( int i = startIndex; i < candidates. size ( ) && sum + candidates[ i] <= target; i++ ) {
sum += candidates[ i] ;
path. push_back ( candidates[ i] ) ;
backtracking ( candidates, target, sum, i) ;
sum -= candidates[ i] ;
path. pop_back ( ) ;
}
}
public :
vector< vector< int >> combinationSum ( vector< int > & candidates, int target) {
result. clear ( ) ;
path. clear ( ) ;
sort ( candidates. begin ( ) , candidates. end ( ) ) ;
backtracking ( candidates, target, 0 , 0 ) ;
return result;
}
} ;
LeetCode 77. 组合
class Solution {
private :
vector< vector< int >> result;
vector< int > path;
void backtracking ( int n, int k, int startIndex) {
if ( path. size ( ) == k) {
result. push_back ( path) ;
return ;
}
for ( int i = startIndex; i <= n - ( k - path. size ( ) ) + 1 ; i++ ) {
path. push_back ( i) ;
backtracking ( n, k, i + 1 ) ;
path. pop_back ( ) ;
}
}
public :
vector< vector< int >> combine ( int n, int k) {
result. clear ( ) ;
path. clear ( ) ;
backtracking ( n, k, 1 ) ;
return result;
}
} ;
LeetCode 216. 组合总和 III
class Solution {
private :
vector< vector< int >> result;
vector< int > path;
void backtracking ( int targetSum, int k, int sum, int startIndex) {
if ( sum > targetSum) {
return ;
}
if ( path. size ( ) == k) {
if ( sum == targetSum) result. push_back ( path) ;
return ;
}
for ( int i = startIndex; i <= 9 - ( k - path. size ( ) ) + 1 ; i++ ) {
sum += i;
path. push_back ( i) ;
backtracking ( targetSum, k, sum, i + 1 ) ;
sum -= i;
path. pop_back ( ) ;
}
}
public :
vector< vector< int >> combinationSum3 ( int k, int n) {
result. clear ( ) ;
path. clear ( ) ;
backtracking ( n, k, 0 , 1 ) ;
return result;
}
} ;
作者:carlsun- 2
链接:https:
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
LeetCode 17. 电话号码的字母组合
class Solution {
private :
const string letterMap[ 10 ] = {
"" ,
"" ,
"abc" ,
"def" ,
"ghi" ,
"jkl" ,
"mno" ,
"pqrs" ,
"tuv" ,
"wxyz" ,
} ;
public :
vector< string> result;
string s;
void backtracking ( const string& digits, int index) {
if ( index == digits. size ( ) ) {
result. push_back ( s) ;
return ;
}
int digit = digits[ index] - '0' ;
string letters = letterMap[ digit] ;
for ( int i = 0 ; i < letters. size ( ) ; i++ ) {
s. push_back ( letters[ i] ) ;
backtracking ( digits, index + 1 ) ;
s. pop_back ( ) ;
}
}
vector< string> letterCombinations ( string digits) {
s. clear ( ) ;
result. clear ( ) ;
if ( digits. size ( ) == 0 ) {
return result;
}
backtracking ( digits, 0 ) ;
return result;
}
} ;
作者:carlsun- 2
链接:https:
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。