36. 有效的数独
void clean(int arr[10])
{
memset(arr, 0, sizeof(int) * 10);
}
bool isValidSudoku(char** board, int boardSize, int* boardColSize)
{
int arr1[10];
int arr2[10];
int i, j;
// 判断横竖
for(i = 0; i < 9; i++) {
clean(arr1);
clean(arr2);
for(j = 0; j < 9; j++) {
if(board[i][j] >= '0' && board[i][j] <= '9') {
if(arr1[board[i][j] - '0']) {
return false;
}
arr1[board[i][j] - '0'] = 1;
}
if(board[j][i] >= '0' && board[j][i] <= '9') {
if(arr2[board[j][i] - '0']) {
return false;
}
arr2[board[j][i] - '0'] = 1;
}
}
}
// 判断田字格
int k, l;
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
clean(arr1);
for(k = 3 * i; k < 3 * i + 3; k++) {
for(l = 3 * j; l < 3 * j + 3; l++){
if(board[k][l] >= '0' && board[k][l] <= '9') {
if(arr1[board[k][l] - '0']) {
return false;
}
arr1[board[k][l]-'0'] = 1;
}
}
}
}
}
return true;
}
37. 解数独(待定)
38. 外观数列
char * countAndSay(int n){
char* mark = (char*)malloc(sizeof(char) * 5000);
char* temp = (char*)malloc(sizeof(char) * 5000);
mark[0] = '1';
mark[1] = '\0';
int i, count, j;
char key, *p;
for(i = 1; i< n; i++) {
j = 0;
key = mark[0];
count = 0;
p = mark;
while(*p != '\0') {
if(*p == key) {
count++;
}
else {
temp[j++] = count + '0';
count = 1;
temp[j++] = key;
key = *p;
}
p++;
}
temp[j++] = count + '0';
temp[j++] = key;
temp[j] = '\0';
strcpy(mark, temp);
}
return mark;
}
39. 组合总和
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
void dfs(int* candidates, int ind, int candidatesSize, int target, int nums[], int n, int** arr, int* returnSize, int** returnColumnSizes)
{
if (target < 0) {
return;
}
if (target == 0) {
arr[*returnSize] = (int*)malloc(sizeof(int) * n);
for (int i = 0; i < n; i++) {
arr[(*returnSize)][i] = nums[i];
}
(*returnColumnSizes)[*returnSize] = n;
(*returnSize)++;
return;
}
for (int i = ind; i < candidatesSize; i++) {
nums[n] = candidates[i];
dfs(candidates, i, candidatesSize, target - nums[n], nums, n + 1, arr, returnSize, returnColumnSizes);
}
}
int** combinationSum(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes)
{
*returnSize = 0;
if (candidatesSize == 0) {
return 0;
}
int** arr = (int**)malloc(sizeof(int*) * 1000000);
*returnColumnSizes = (int*)malloc(sizeof(int) * 1000000);
int nums[target];
dfs(candidates, 0, candidatesSize, target, nums, 0, arr, returnSize, returnColumnSizes);
return arr;
}
40. 组合总和 II
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int cmp(const void* a, const void* b)
{
return *(int*)a - *(int*)b;
}
void dfs(int* candidates, int ind, int candidatesSize, int target, int nums[], int n, int** arr, int* returnSize, int** returnColumnSizes)
{
if (target < 0) {
return ;
}
if (target == 0) {
arr[*returnSize] = (int*)malloc(sizeof(int) * n);
for (int i = 0; i < n; i++) {
arr[(*returnSize)][i] = nums[i];
}
(*returnColumnSizes)[*returnSize] = n;
(*returnSize)++;
return ;
}
int i = ind;
while (i < candidatesSize) {
nums[n] = candidates[i];
dfs(candidates, i + 1, candidatesSize, target - nums[n], nums, n + 1, arr, returnSize, returnColumnSizes);
while (i < candidatesSize - 1 && candidates[i] == candidates[i + 1]) {
i++;
}
i++;
}
}
int** combinationSum2(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes)
{
*returnSize = 0;
if (candidatesSize == 0) {
return 0;
}
qsort(candidates, candidatesSize, sizeof(int), cmp);
int** arr = (int**)malloc(sizeof(int*) * 1000000);
(*returnColumnSizes) = (int*)malloc(sizeof(int) * 1000000);
int nums[target];
dfs(candidates, 0, candidatesSize, target, nums, 0, arr, returnSize, returnColumnSizes);
return arr;
}