mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Merge pull request #973 from KingArthur0205/remote
添加 0051.N皇后.md C语言版本, 0024.两两交换链表中的节点.md C语言递归版本
This commit is contained in:
@ -94,8 +94,23 @@ 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;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
```c
|
||||
//迭代版本
|
||||
struct ListNode* swapPairs(struct ListNode* head){
|
||||
//使用双指针避免使用中间变量
|
||||
typedef struct ListNode ListNode;
|
||||
|
@ -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;
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
Reference in New Issue
Block a user