# An easy and beautiful code to understand by C++

• //| 1 | 2 | 3 |
//| 4 | 5 | 6 |
//| 7 | 8 | 9 |
//0 => 1,2,3,4,5,6,7,8,9

//[2]
//3 !=> 1
//1 !=> 3

//[6]
//3 !=> 9
//9 !=> 3

//[8]
//7 !=> 9
//9 !=> 7

//[4]
//7 !=> 1
//1 !=> 7

//[5]
//1 !=> 9
//9 !=> 1

//3 !=> 7
//7 !=> 3

//2 !=> 8
//8 !=> 2

//4 !=> 6
//6 !=> 4

int sum = 0;

bool correct(int n[] , int num , int p , int q){
//printf("%d %d" , p , q);
if (p == 0) return true;
if (p == q) return false;
int tmp[10];
for (int i = 0;i < 10;i++) tmp[i] = 0;
//only distinct
for (int i = 0;i < num;i++)
if (q == n[i])
return false;
else tmp[n[i]] = 1;

int mi = p < q ? p : q;
int ma = p < q ? q : p;
if (mi == 1 && ma == 3) return tmp[2];
if (mi == 3 && ma == 9) return tmp[6];
if (mi == 7 && ma == 9) return tmp[8];
if (mi == 1 && ma == 7) return tmp[4];
if (p + q == 10) return tmp[5];

return true;
}

void go(int n[] , int pre , int num , int min , int max){
if (num >= min && num <= max){
//printf("\t");
sum++;

// for (int i = 0;i < num;i++)
//     printf("%d " , n[i]);
// printf("\n");

}
if (num < max){
for (int i = 1;i <= 9;i++){
if (correct(n , num , pre , i)){
//printf("true\n");
n[num] = i;
go(n , i , num + 1 , min , max);
}else{
//printf("false\n");
}
}
}else{
return;
}
}

int numberOfPatterns(int m, int n) {
sum = 0;
int nn[1000];
go(nn , 0 , 0 , m , n);
return sum;
}

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.