From 9356debd174b412729496b9ee254597967d551b2 Mon Sep 17 00:00:00 2001 From: Haoting <1165101405@qq.com> Date: Sat, 27 Apr 2024 17:08:54 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A00104=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6=20C?= =?UTF-8?q?=E8=AF=AD=E8=A8=80=E8=BF=AD=E4=BB=A3=E6=B3=95=E2=80=94=E2=80=94?= =?UTF-8?q?=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0104.二叉树的最大深度.md | 37 ++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 0f93cb0f..607e195b 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -829,7 +829,42 @@ int maxDepth(struct TreeNode* root){ return depth; } ``` - +二叉树最大深度迭代——后序遍历实现 +```c +int maxDepth(struct TreeNode *root) +{ + if(root == NULL) + return 0; + struct TreeNode *stack[10000] = {}; + int top = -1; + struct TreeNode *p = root, *r = NULL; // r指向上一个被访问的结点 + int depth = 0, maxDepth = -1; + while(p != NULL || top >= 0) + { + if(p != NULL) + { + stack[++top] = p; + depth++; + p = p->left; + } + else + { + p = stack[top]; + if(p->right != NULL && p->right != r) // 右子树未被访问 + p = p->right; + else + { + if(depth >= maxDepth) maxDepth = depth; + p = stack[top--]; + depth--; + r = p; + p = NULL; + } + } + } + return maxDepth; +} +``` ### Swift: 104.二叉树的最大深度 From 8dcfd4f319c219fca9db3268f3cecff3a3156b59 Mon Sep 17 00:00:00 2001 From: Haoting <1165101405@qq.com> Date: Sat, 27 Apr 2024 17:10:20 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A00055=E5=8F=B3=E6=97=8B?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=20C=E8=AF=AD=E8=A8=80=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/kamacoder/0055.右旋字符串.md | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/problems/kamacoder/0055.右旋字符串.md b/problems/kamacoder/0055.右旋字符串.md index 71371860..22be955f 100644 --- a/problems/kamacoder/0055.右旋字符串.md +++ b/problems/kamacoder/0055.右旋字符串.md @@ -253,6 +253,47 @@ func main(){ ``` +### C: +```C +#include +#include + +void reverse(char *s, int left, int right) +{ + while(left <= right) + { + char c = s[left]; + s[left] = s[right]; + s[right] = c; + left++; + right--; + } +} + +void rightRotate(char *s, int k) +{ + int len = strlen(s); + // 先局部反转再整体反转 + reverse(s, 0, len - k - 1); // 反转前部分 + reverse(s, len - k, len - 1); // 反转后部分:后k位 + reverse(s, 0, len - 1); // 整体反转 +} + +int main() +{ + + int k; + scanf("%d", &k); + char s[10000]; + scanf("%s", s); + + rightRotate(s, k); + printf("%s\n", s); + + return 0; +} +``` + ### JavaScript: From b06601c58c08a8647c64af82034e41f1b06d8618 Mon Sep 17 00:00:00 2001 From: markwang Date: Sun, 28 Apr 2024 15:18:01 +0800 Subject: [PATCH 3/5] =?UTF-8?q?54.=E8=9E=BA=E6=97=8B=E7=9F=A9=E9=98=B5?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0Go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0054.螺旋矩阵.md | 76 +++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/problems/0054.螺旋矩阵.md b/problems/0054.螺旋矩阵.md index 4d54ccd6..175ae147 100644 --- a/problems/0054.螺旋矩阵.md +++ b/problems/0054.螺旋矩阵.md @@ -348,6 +348,82 @@ class Solution(object): return print_list ``` +### Go: + +```go +func spiralOrder(matrix [][]int) []int { + rows := len(matrix) + if rows == 0 { + return []int{} + } + columns := len(matrix[0]) + if columns == 0 { + return []int{} + } + res := make([]int, rows * columns) + startx, starty := 0, 0 // 定义每循环一个圈的起始位置 + loop := min(rows, columns) / 2 + mid := min(rows, columns) / 2 + count := 0 // 用来给矩阵中每一个空格赋值 + offset := 1 // 每一圈循环,需要控制每一条边遍历的长度 + for loop > 0 { + i, j := startx, starty + + // 模拟填充上行从左到右(左闭右开) + for ; j < starty + columns - offset; j++ { + res[count] = matrix[startx][j] + count++ + } + // 模拟填充右列从上到下(左闭右开) + for ; i < startx + rows - offset; i++ { + res[count] = matrix[i][j] + count++ + } + // 模拟填充下行从右到左(左闭右开) + for ; j > starty; j-- { + res[count] = matrix[i][j] + count++ + } + // 模拟填充左列从下到上(左闭右开) + for ; i > startx; i-- { + res[count] = matrix[i][starty] + count++ + } + + // 第二圈开始的时候,起始位置要各自加1, 例如:第一圈起始位置是(0, 0),第二圈起始位置是(1, 1) + startx++ + starty++ + + // offset 控制每一圈里每一条边遍历的长度 + offset += 2 + loop-- + } + + // 如果min(rows, columns)为奇数的话,需要单独给矩阵最中间的位置赋值 + if min(rows, columns) % 2 == 1 { + if rows > columns { + for i := mid; i < mid + rows - columns + 1; i++ { + res[count] = matrix[i][mid] + count++ + } + } else { + for i := mid; i < mid + columns - rows + 1; i++ { + res[count] = matrix[mid][i] + count++ + } + } + } + return res +} + +func min(x, y int) int { + if x < y { + return x + } + return y +} +``` +

From fda179fd7f0789e33f65b6bc5cb1c517a8a83104 Mon Sep 17 00:00:00 2001 From: markwang Date: Sun, 28 Apr 2024 15:21:11 +0800 Subject: [PATCH 4/5] =?UTF-8?q?54.=E8=9E=BA=E6=97=8B=E7=9F=A9=E9=98=B5?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0Go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0054.螺旋矩阵.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0054.螺旋矩阵.md b/problems/0054.螺旋矩阵.md index 175ae147..022eed66 100644 --- a/problems/0054.螺旋矩阵.md +++ b/problems/0054.螺旋矩阵.md @@ -348,7 +348,7 @@ class Solution(object): return print_list ``` -### Go: +### Go ```go func spiralOrder(matrix [][]int) []int { From 2e489ea3bbe82a96a88e2c55ae92e19f8564f936 Mon Sep 17 00:00:00 2001 From: Jeff Lin Date: Sun, 28 Apr 2024 15:58:38 -0700 Subject: [PATCH 5/5] =?UTF-8?q?Update=200017.=E7=94=B5=E8=AF=9D=E5=8F=B7?= =?UTF-8?q?=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更正时间复杂度说明。 --- problems/0017.电话号码的字母组合.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index e25d15d5..3b26066a 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -180,7 +180,7 @@ public: } }; ``` -* 时间复杂度: O(3^m * 4^n),其中 m 是对应四个字母的数字个数,n 是对应三个字母的数字个数 +* 时间复杂度: O(3^m * 4^n),其中 m 是对应三个字母的数字个数,n 是对应四个字母的数字个数 * 空间复杂度: O(3^m * 4^n) 一些写法,是把回溯的过程放在递归函数里了,例如如下代码,我可以写成这样:(注意注释中不一样的地方)