From 7773bd9470e2824a054de8fea83a100c7f12362c Mon Sep 17 00:00:00 2001 From: ArthurP Date: Wed, 29 Dec 2021 21:56:11 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200051.N=E7=9A=87?= =?UTF-8?q?=E5=90=8E.md=20C=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0051.N皇后.md | 125 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/problems/0051.N皇后.md b/problems/0051.N皇后.md index 7302887a..d658226b 100644 --- a/problems/0051.N皇后.md +++ b/problems/0051.N皇后.md @@ -521,5 +521,130 @@ func solveNQueens(_ n: Int) -> [[String]] { } ``` +### C +```c +char ***ans; +char **path; +int ansTop, pathTop; +//将path中元素复制到ans中 +void copyPath(int n) { + char **tempPath = (char**)malloc(sizeof(char*) * pathTop); + int i; + for(i = 0; i < pathTop; ++i) { + tempPath[i] = (char*)malloc(sizeof(char) * n + 1); + int j; + for(j = 0; j < n; ++j) + tempPath[i][j] = path[i][j]; + tempPath[i][j] = '\0'; + + } + ans[ansTop++] = tempPath; +} + +//判断当前位置是否有效(是否不被其它皇后影响) +int isValid(int x, int y, int n) { + int i, j; + //检查同一行以及同一列是否有效 + for(i = 0; i < n; ++i) { + if(path[y][i] == 'Q' || path[i][x] == 'Q') + return 0; + } + //下面两个for循环检查斜角45度是否有效 + i = y - 1; + j = x - 1; + while(i >= 0 && j >= 0) { + if(path[i][j] == 'Q') + return 0; + --i, --j; + } + + i = y + 1; + j = x + 1; + while(i < n && j < n) { + if(path[i][j] == 'Q') + return 0; + ++i, ++j; + } + + //下面两个for循环检查135度是否有效 + i = y - 1; + j = x + 1; + while(i >= 0 && j < n) { + if(path[i][j] == 'Q') + return 0; + --i, ++j; + } + + i = y + 1; + j = x -1; + while(j >= 0 && i < n) { + if(path[i][j] == 'Q') + return 0; + ++i, --j; + } + return 1; +} + +void backTracking(int n, int depth) { + //若path中有四个元素,将其拷贝到ans中。从当前层返回 + if(pathTop == n) { + copyPath(n); + return; + } + + //遍历横向棋盘 + int i; + for(i = 0; i < n; ++i) { + //若当前位置有效 + if(isValid(i, depth, n)) { + //在当前位置放置皇后 + path[depth][i] = 'Q'; + //path中元素数量+1 + ++pathTop; + + backTracking(n, depth + 1); + //进行回溯 + path[depth][i] = '.'; + //path中元素数量-1 + --pathTop; + } + } +} + +//初始化存储char*数组path,将path中所有元素设为'.' +void initPath(int n) { + int i, j; + for(i = 0; i < n; i++) { + //为path中每个char*开辟空间 + path[i] = (char*)malloc(sizeof(char) * n + 1); + //将path中所有字符设为'.' + for(j = 0; j < n; j++) + path[i][j] = '.'; + //在每个字符串结尾加入'\0' + path[i][j] = '\0'; + } +} + +char *** solveNQueens(int n, int* returnSize, int** returnColumnSizes){ + //初始化辅助变量 + ans = (char***)malloc(sizeof(char**) * 400); + path = (char**)malloc(sizeof(char*) * n); + ansTop = pathTop = 0; + + //初始化path数组 + initPath(n); + backTracking(n, 0); + + //设置返回数组大小 + *returnSize = ansTop; + int i; + *returnColumnSizes = (int*)malloc(sizeof(int) * ansTop); + for(i = 0; i < ansTop; ++i) { + (*returnColumnSizes)[i] = n; + } + return ans; +} +``` + -----------------------
From ac735d918b22ee025dddf7d3df038af5a5ba2b39 Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Sun, 2 Jan 2022 14:30:25 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200024.=E4=B8=A4?= =?UTF-8?q?=E4=B8=A4=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9.md=20C=E8=AF=AD=E8=A8=80=E9=80=92=E5=BD=92?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0024.两两交换链表中的节点.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md index 9b9c6689..f1bfa5b2 100644 --- a/problems/0024.两两交换链表中的节点.md +++ b/problems/0024.两两交换链表中的节点.md @@ -94,8 +94,21 @@ C: * struct ListNode *next; * }; */ +//递归版本 +struct ListNode* swapPairs(struct ListNode* head){ + //递归结束条件:头节点不存在或头节点的下一个节点不存在。此时不需要交换,直接返回head + if(!head || !head->next) + return head; + //创建一个节点指针类型保存头结点下一个节点 + struct ListNode *newHead = head->next; + //更改头结点+2位节点后的值,并将头结点的next指针指向这个更改过的list + head->next = swapPairs(newHead->next); + //将新的头结点的next指针指向老的头节点 + newHead->next = head; + return newHead; +} - +//迭代版本 struct ListNode* swapPairs(struct ListNode* head){ //使用双指针避免使用中间变量 typedef struct ListNode ListNode; From e3f9268351ddc9b8e6c3edf776ba73dcdf7d5688 Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Sun, 2 Jan 2022 18:33:21 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=94=B9=200024.=E4=B8=A4?= =?UTF-8?q?=E4=B8=A4=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9.md=20C=E8=AF=AD=E8=A8=80=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0024.两两交换链表中的节点.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md index f1bfa5b2..01abc7b4 100644 --- a/problems/0024.两两交换链表中的节点.md +++ b/problems/0024.两两交换链表中的节点.md @@ -107,7 +107,9 @@ struct ListNode* swapPairs(struct ListNode* head){ newHead->next = head; return newHead; } +``` +```c //迭代版本 struct ListNode* swapPairs(struct ListNode* head){ //使用双指针避免使用中间变量