From fb9186fc79c233a9cf06f9e5ecc3070f1d1c3332 Mon Sep 17 00:00:00 2001 From: C_W Date: Thu, 12 Dec 2024 12:15:00 +1100 Subject: [PATCH 01/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A00459=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E7=9A=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2=20C=20=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/0459.重复的子字符串.md | 46 ++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md index 2be8922b..de0e6e4d 100644 --- a/problems/0459.重复的子字符串.md +++ b/problems/0459.重复的子字符串.md @@ -879,6 +879,52 @@ public int[] GetNext(string s) } ``` +### C + +```c +// 前缀表不减一 +int *build_next(char* s, int len) { + + int *next = (int *)malloc(len * sizeof(int)); + assert(next); + + // 初始化前缀表 + next[0] = 0; + + // 构建前缀表表 + int i = 1, j = 0; + while (i < len) { + if (s[i] == s[j]) { + j++; + next[i] = j; + i++; + } else if (j > 0) { + j = next[j - 1]; + } else { + next[i] = 0; + i++; + } + } + return next; +} + +bool repeatedSubstringPattern(char* s) { + + int len = strlen(s); + int *next = build_next(s, len); + bool result = false; + + // 检查最小重复片段能否被长度整除 + if (next[len - 1]) { + result = len % (len - next[len - 1]) == 0; + } + + free(next); + return result; +} + +``` +

From d6f7f3adbcd2532fafe6ffc06efc4e3d01f8d1ee Mon Sep 17 00:00:00 2001 From: C_W Date: Thu, 12 Dec 2024 22:32:25 +1100 Subject: [PATCH 02/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200225.=E7=94=A8?= =?UTF-8?q?=E9=98=9F=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88.md=20C=20?= =?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/0225.用队列实现栈.md | 89 +++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index f0fe3a3c..73d9db1b 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -1277,6 +1277,95 @@ impl MyStack { } ``` +### C: + +> C:单队列 + +```c +typedef struct Node { + int val; + struct Node *next; +} Node_t; + +// 用单向链表实现queue +typedef struct { + Node_t *head; + Node_t *foot; + int size; +} MyStack; + +MyStack* myStackCreate() { + MyStack *obj = (MyStack *)malloc(sizeof(MyStack)); + assert(obj); + obj->head = NULL; + obj->foot = NULL; + obj->size = 0; + return obj; +} + +void myStackPush(MyStack* obj, int x) { + + Node_t *temp = (Node_t *)malloc(sizeof(Node_t)); + assert(temp); + temp->val = x; + temp->next = NULL; + + // 添加至queue末尾 + if (obj->foot) { + obj->foot->next = temp; + } else { + obj->head = temp; + } + obj->foot = temp; + obj->size++; +} + +int myStackPop(MyStack* obj) { + + // 获取末尾元素 + int target = obj->foot->val; + + if (obj->head == obj->foot) { + free(obj->foot); + obj->head = NULL; + obj->foot = NULL; + } else { + + Node_t *prev = obj->head; + // 移动至queue尾部节点前一个节点 + while (prev->next != obj->foot) { + prev = prev->next; + } + + free(obj->foot); + obj->foot = prev; + obj->foot->next = NULL; + } + + obj->size--; + return target; +} + +int myStackTop(MyStack* obj) { + return obj->foot->val; +} + +bool myStackEmpty(MyStack* obj) { + return obj->size == 0; +} + +void myStackFree(MyStack* obj) { + Node_t *curr = obj->head; + while (curr != NULL) { + Node_t *temp = curr->next; + free(curr); + curr = temp; + } + free(obj); +} + +``` +

From d66e733d6c66b37baec8acd0d42f8161a07bf8fc Mon Sep 17 00:00:00 2001 From: C_W Date: Thu, 12 Dec 2024 22:33:13 +1100 Subject: [PATCH 03/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200150.=E9=80=86?= =?UTF-8?q?=E6=B3=A2=E5=85=B0=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC?= =?UTF-8?q?.md=20C=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0150.逆波兰表达式求值.md | 61 +++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md index 7d4031d7..0f1e9c23 100644 --- a/problems/0150.逆波兰表达式求值.md +++ b/problems/0150.逆波兰表达式求值.md @@ -502,6 +502,67 @@ impl Solution { } ``` +### C: + +```c +int str_to_int(char *str) { + // string转integer + int num = 0, tens = 1; + for (int i = strlen(str) - 1; i >= 0; i--) { + if (str[i] == '-') { + num *= -1; + break; + } + num += (str[i] - '0') * tens; + tens *= 10; + } + return num; +} + +int evalRPN(char** tokens, int tokensSize) { + + int *stack = (int *)malloc(tokensSize * sizeof(int)); + assert(stack); + int stackTop = 0; + + for (int i = 0; i < tokensSize; i++) { + char symbol = (tokens[i])[0]; + if (symbol < '0' && (tokens[i])[1] == '\0') { + + // pop两个数字 + int num1 = stack[--stackTop]; + int num2 = stack[--stackTop]; + + // 计算结果 + int result; + if (symbol == '+') { + result = num1 + num2; + } else if (symbol == '-') { + result = num2 - num1; + } else if (symbol == '/') { + result = num2 / num1; + } else { + result = num1 * num2; + } + + // push回stack + stack[stackTop++] = result; + + } else { + + // push数字进stack + int num = str_to_int(tokens[i]); + stack[stackTop++] = num; + + } + } + + int result = stack[0]; + free(stack); + return result; +} +``` +

From 739e3f891ce199d7258fa4a192b7fe3dd10a9251 Mon Sep 17 00:00:00 2001 From: C_W Date: Fri, 13 Dec 2024 11:43:42 +1100 Subject: [PATCH 04/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200239.=E6=BB=91?= =?UTF-8?q?=E5=8A=A8=E7=AA=97=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC.md=20C?= =?UTF-8?q?=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0239.滑动窗口最大值.md | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index caa24d8d..9bb3494d 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -890,6 +890,38 @@ public: }; ``` +### C + +```c +int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize) { + *returnSize = numsSize - k + 1; + int *res = (int*)malloc((*returnSize) * sizeof(int)); + assert(res); + int *deque = (int*)malloc(numsSize * sizeof(int)); + assert(deque); + int front = 0, rear = 0, idx = 0; + + for (int i = 0 ; i < numsSize ; i++) { + while (front < rear && deque[front] <= i - k) { + front++; + } + + while (front < rear && nums[deque[rear - 1]] <= nums[i]) { + rear--; + } + + deque[rear++] = i; + + if (i >= k - 1) { + res[idx++] = nums[deque[front]]; + } + } + + return res; +} + +``` +

From 3ce802897f9cf19ef158cb197f031c1cf3cb8baf Mon Sep 17 00:00:00 2001 From: Murphy Tian Date: Mon, 16 Dec 2024 15:38:43 +0800 Subject: [PATCH 05/20] [Fix][DP][Target Sum] python 2D version align with the dp equation --- problems/0494.目标和.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index dda3ad75..eef5ceb6 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -671,18 +671,26 @@ class Solution: # 创建二维动态规划数组,行表示选取的元素数量,列表示累加和 dp = [[0] * (target_sum + 1) for _ in range(len(nums) + 1)] + dp = [[0] * (target_sum + 1) for _ in range(len(nums))] # 初始化状态 dp[0][0] = 1 + if nums[0] <= target_sum: + dp[0][nums[0]] = 1 + numZero = 0 + for i in range(len(nums)): + if nums[i] == 0: + numZero += 1 + dp[i][0] = int(math.pow(2, numZero)) # 动态规划过程 - for i in range(1, len(nums) + 1): + for i in range(1, len(nums)): for j in range(target_sum + 1): dp[i][j] = dp[i - 1][j] # 不选取当前元素 if j >= nums[i - 1]: - dp[i][j] += dp[i - 1][j - nums[i - 1]] # 选取当前元素 + dp[i][j] += dp[i - 1][j - nums[i]] # 选取当前元素 - return dp[len(nums)][target_sum] # 返回达到目标和的方案数 + return dp[len(nums)-1][target_sum] # 返回达到目标和的方案数 ``` From e93f00aef737dad56bf4656a02c17cb66e214fcc Mon Sep 17 00:00:00 2001 From: ForsakenDelusion <144082461+ForsakenDelusion@users.noreply.github.com> Date: Sat, 21 Dec 2024 18:06:53 +0800 Subject: [PATCH 06/20] =?UTF-8?q?0235.=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96?= =?UTF-8?q?=E5=85=88=E4=B8=80=E5=A4=84=E9=94=99=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0235.二叉搜索树的最近公共祖先.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md index 192bb031..3911261a 100644 --- a/problems/0235.二叉搜索树的最近公共祖先.md +++ b/problems/0235.二叉搜索树的最近公共祖先.md @@ -99,7 +99,7 @@ if (cur == NULL) return cur; * 确定单层递归的逻辑 -在遍历二叉搜索树的时候就是寻找区间[p->val, q->val](注意这里是左闭又闭) +在遍历二叉搜索树的时候就是寻找区间[p->val, q->val](注意这里是左闭右闭) 那么如果 cur->val 大于 p->val,同时 cur->val 大于q->val,那么就应该向左遍历(说明目标区间在左子树上)。 From 173ebe75726cf41260ccc8824c73fcb78dfdcd8f Mon Sep 17 00:00:00 2001 From: "Zhen (Evan) Wang" <273509239@qq.com> Date: Wed, 25 Dec 2024 11:14:51 +0800 Subject: [PATCH 07/20] =?UTF-8?q?=E6=9B=B4=E6=96=B00059.=E8=9E=BA=E6=97=8B?= =?UTF-8?q?=E7=9F=A9=E9=98=B5II=E7=9A=84C#=E7=89=88=E6=9C=AC=E4=BF=9D?= =?UTF-8?q?=E8=AF=81=E4=B8=8EC++=E7=89=88=E6=9C=AC=E7=9A=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BF=9D=E6=8C=81=E4=B8=80=E8=87=B4=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0059.螺旋矩阵II.md | 77 +++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 19 deletions(-) diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md index c59ec033..94966126 100644 --- a/problems/0059.螺旋矩阵II.md +++ b/problems/0059.螺旋矩阵II.md @@ -715,26 +715,65 @@ object Solution { ### C#: ```csharp -public class Solution { - public int[][] GenerateMatrix(int n) { - int[][] answer = new int[n][]; - for(int i = 0; i < n; i++) - answer[i] = new int[n]; - int start = 0; - int end = n - 1; - int tmp = 1; - while(tmp < n * n) - { - for(int i = start; i < end; i++) answer[start][i] = tmp++; - for(int i = start; i < end; i++) answer[i][end] = tmp++; - for(int i = end; i > start; i--) answer[end][i] = tmp++; - for(int i = end; i > start; i--) answer[i][start] = tmp++; - start++; - end--; - } - if(n % 2 == 1) answer[n / 2][n / 2] = tmp; - return answer; +public int[][] GenerateMatrix(int n) +{ + // 参考Carl的代码随想录里面C++的思路 + // https://www.programmercarl.com/0059.%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5II.html#%E6%80%9D%E8%B7%AF + int startX = 0, startY = 0; // 定义每循环一个圈的起始位置 + int loop = n / 2; // 每个圈循环几次,例如n为奇数3,那么loop = 1 只是循环一圈,矩阵中间的值需要单独处理 + int count = 1; // 用来给矩阵每个空格赋值 + int mid = n / 2; // 矩阵中间的位置,例如:n为3, 中间的位置就是(1,1),n为5,中间位置为(2, 2) + int offset = 1;// 需要控制每一条边遍历的长度,每次循环右边界收缩一位 + + // 构建result二维数组 + int[][] result = new int[n][]; + for (int k = 0; k < n; k++) + { + result[k] = new int[n]; } + + int i = 0, j = 0; // [i,j] + while (loop > 0) + { + i = startX; + j = startY; + // 四个For循环模拟转一圈 + // 第一排,从左往右遍历,不取最右侧的值(左闭右开) + for (; j < n - offset; j++) + { + result[i][j] = count++; + } + // 右侧的第一列,从上往下遍历,不取最下面的值(左闭右开) + for (; i < n - offset; i++) + { + result[i][j] = count++; + } + + // 最下面的第一行,从右往左遍历,不取最左侧的值(左闭右开) + for (; j > startY; j--) + { + result[i][j] = count++; + } + + // 左侧第一列,从下往上遍历,不取最左侧的值(左闭右开) + for (; i > startX; i--) + { + result[i][j] = count++; + } + // 第二圈开始的时候,起始位置要各自加1, 例如:第一圈起始位置是(0, 0),第二圈起始位置是(1, 1) + startX++; + startY++; + + // offset 控制每一圈里每一条边遍历的长度 + offset++; + loop--; + } + if (n % 2 == 1) + { + // n 为奇数 + result[mid][mid] = count; + } + return result; } ``` From 7a1d070c257c16bf01aaf4b95bded65393bc3286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=AE=A3=E9=AD=81?= <13963268+cheng-xuankui@user.noreply.gitee.com> Date: Thu, 26 Dec 2024 20:49:16 +0800 Subject: [PATCH 08/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A019.=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN?= =?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9,=E6=B7=BB=E5=8A=A0=E4=BA=86java?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E4=BD=BF=E7=94=A8=E9=80=92=E5=BD=92=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=88=A0=E9=99=A4=E5=80=92=E6=95=B0=E7=AC=AC?= =?UTF-8?q?N=E4=B8=AA=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0019.删除链表的倒数第N个节点.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index fafef1f2..16312d0f 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -129,6 +129,36 @@ class Solution { } ``` + +```java +class Solution { + public ListNode removeNthFromEnd(ListNode head, int n) { + // 创建一个新的哑节点,指向原链表头 + ListNode s = new ListNode(-1, head); + // 递归调用remove方法,从哑节点开始进行删除操作 + remove(s, n); + // 返回新链表的头(去掉可能的哑节点) + return s.next; + } + + public int remove(ListNode p, int n) { + // 递归结束条件:如果当前节点为空,返回0 + if (p == null) { + return 0; + } + // 递归深入到下一个节点 + int net = remove(p.next, n); + // 如果当前节点是倒数第n个节点,进行删除操作 + if (net == n) { + p.next = p.next.next; + } + // 返回当前节点的总深度 + return net + 1; + } +} +``` + + ### Python: ```python From b72609d65ed2c41212ba39bba57f21615be6077e Mon Sep 17 00:00:00 2001 From: Jian Date: Sat, 4 Jan 2025 01:59:28 +0800 Subject: [PATCH 09/20] =?UTF-8?q?0018=E5=9B=9B=E6=95=B0=E4=B9=8B=E5=92=8C?= =?UTF-8?q?=20=E4=BF=AE=E6=94=B9=E6=80=9D=E8=B7=AF=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E6=AE=B5=E6=9C=AB=E5=B0=BE=E6=8F=8F=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0018.四数之和.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/problems/0018.四数之和.md b/problems/0018.四数之和.md index 64923e41..fb8557fa 100644 --- a/problems/0018.四数之和.md +++ b/problems/0018.四数之和.md @@ -35,7 +35,7 @@ 四数之和,和[15.三数之和](https://programmercarl.com/0015.三数之和.html)是一个思路,都是使用双指针法, 基本解法就是在[15.三数之和](https://programmercarl.com/0015.三数之和.html) 的基础上再套一层for循环。 -但是有一些细节需要注意,例如: 不要判断`nums[k] > target` 就返回了,三数之和 可以通过 `nums[i] > 0` 就返回了,因为 0 已经是确定的数了,四数之和这道题目 target是任意值。比如:数组是`[-4, -3, -2, -1]`,`target`是`-10`,不能因为`-4 > -10`而跳过。但是我们依旧可以去做剪枝,逻辑变成`nums[i] > target && (nums[i] >=0 || target >= 0)`就可以了。 +但是有一些细节需要注意,例如: 不要判断`nums[k] > target` 就返回了,三数之和 可以通过 `nums[i] > 0` 就返回了,因为 0 已经是确定的数了,四数之和这道题目 target是任意值。比如:数组是`[-4, -3, -2, -1]`,`target`是`-10`,不能因为`-4 > -10`而跳过。但是我们依旧可以去做剪枝,逻辑变成`nums[k] > target && (nums[k] >=0 || target >= 0)`就可以了。 [15.三数之和](https://programmercarl.com/0015.三数之和.html)的双指针解法是一层for循环num[i]为确定值,然后循环内有left和right下标作为双指针,找到nums[i] + nums[left] + nums[right] == 0。 @@ -802,3 +802,4 @@ end + From d5e0827abeabbcc97b341e99231966bfbafaf7cd Mon Sep 17 00:00:00 2001 From: Jian Date: Wed, 8 Jan 2025 01:04:07 +0800 Subject: [PATCH 10/20] =?UTF-8?q?0020=E6=9C=89=E6=95=88=E7=9A=84=E6=8B=AC?= =?UTF-8?q?=E5=8F=B7=20Java=E6=9B=B4=E7=AE=80=E5=8D=95=E6=98=93=E6=87=82?= =?UTF-8?q?=E7=9A=84=E6=96=B0=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0020.有效的括号.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index f2f5cdd1..493e2871 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -172,6 +172,29 @@ class Solution { } ``` +```java +// 解法二 +// 对应的另一半一定在栈顶 +class Solution { + public boolean isValid(String s) { + Stack stack = new Stack<>(); + for(char c : s.toCharArray()){ + // 有对应的另一半就直接消消乐 + if(c == ')' && !stack.isEmpty() && stack.peek() == '(') + stack.pop(); + else if(c == '}' && !stack.isEmpty() && stack.peek() == '{') + stack.pop(); + else if(c == ']' && !stack.isEmpty() && stack.peek() == '[') + stack.pop(); + else + stack.push(c);// 没有匹配的就放进去 + } + + return stack.isEmpty(); + } +} +``` + ### Python: ```python From a7ad0cd812649e0cf1928d9c4e54bc4369588a19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Sat, 11 Jan 2025 19:18:46 +0800 Subject: [PATCH 11/20] =?UTF-8?q?=E4=BF=AE=E6=94=B9leetcode-master\problem?= =?UTF-8?q?s\kamacoder\0044.=E5=BC=80=E5=8F=91=E5=95=86=E8=B4=AD=E4=B9=B0?= =?UTF-8?q?=E5=9C=9F=E5=9C=B0.md=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/kamacoder/0044.开发商购买土地.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/problems/kamacoder/0044.开发商购买土地.md b/problems/kamacoder/0044.开发商购买土地.md index 739e2cad..efad56da 100644 --- a/problems/kamacoder/0044.开发商购买土地.md +++ b/problems/kamacoder/0044.开发商购买土地.md @@ -212,13 +212,14 @@ public class Main { int horizontalCut = 0; for (int i = 0; i < n; i++) { horizontalCut += horizontal[i]; - result = Math.min(result, Math.abs(sum - 2 * horizontalCut)); + result = Math.min(result, Math.abs((sum - horizontalCut) - horizontalCut)); + // 更新result。其中,horizontalCut表示前i行的和,sum - horizontalCut表示剩下的和,作差、取绝对值,得到题目需要的“A和B各自的子区域内的土地总价值之差”。下同。 } int verticalCut = 0; for (int j = 0; j < m; j++) { verticalCut += vertical[j]; - result = Math.min(result, Math.abs(sum - 2 * verticalCut)); + result = Math.min(result, Math.abs((sum - verticalCut) - verticalCut)); } System.out.println(result); From cb5b6e524197e1e2f66bcd3b157b37bfbc726803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Sun, 12 Jan 2025 11:54:23 +0800 Subject: [PATCH 12/20] =?UTF-8?q?=E4=BF=AE=E6=94=B9leetcode-master\problem?= =?UTF-8?q?s\0349.=E4=B8=A4=E4=B8=AA=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4?= =?UTF-8?q?=E9=9B=86.md=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0349.两个数组的交集.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 77dfc50a..93fa0931 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -123,6 +123,9 @@ public: ### Java: 版本一:使用HashSet ```Java +// 时间复杂度O(n+m+k) 空间复杂度O(n+k) +// 其中n是数组nums1的长度,m是数组nums2的长度,k是交集元素的个数 + import java.util.HashSet; import java.util.Set; @@ -145,8 +148,15 @@ class Solution { } //方法1:将结果集合转为数组 - - return resSet.stream().mapToInt(x -> x).toArray(); + return res.stream().mapToInt(Integer::intValue).toArray(); + /** + * 将 Set 转换为 int[] 数组: + * 1. stream() : Collection 接口的方法,将集合转换为 Stream + * 2. mapToInt(Integer::intValue) : + * - 中间操作,将 Stream 转换为 IntStream + * - 使用方法引用 Integer::intValue,将 Integer 对象拆箱为 int 基本类型 + * 3. toArray() : 终端操作,将 IntStream 转换为 int[] 数组。 + */ //方法2:另外申请一个数组存放setRes中的元素,最后返回数组 int[] arr = new int[resSet.size()]; @@ -538,3 +548,4 @@ end + From 61c04cdc433050e09f8f67aeb1fc45454a7f1e99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Mon, 13 Jan 2025 15:47:55 +0800 Subject: [PATCH 13/20] =?UTF-8?q?=E4=BF=AE=E6=94=B90383.=E8=B5=8E=E9=87=91?= =?UTF-8?q?=E4=BF=A1.md=E7=9A=84=E6=97=B6=E9=97=B4=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0383.赎金信.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index eb83d3ec..1d739173 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -104,7 +104,7 @@ public: }; ``` -* 时间复杂度: O(n) +* 时间复杂度: O(m+n),其中m表示ransomNote的长度,n表示magazine的长度 * 空间复杂度: O(1) @@ -470,3 +470,4 @@ bool canConstruct(char* ransomNote, char* magazine) { + From 13cb15cad6d548a99b4f4b427a3d57f91d5ce3b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Mon, 13 Jan 2025 16:33:28 +0800 Subject: [PATCH 14/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A00018.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md=E7=9A=84Java=E7=89=88=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0018.四数之和.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/problems/0018.四数之和.md b/problems/0018.四数之和.md index 64923e41..acf8263c 100644 --- a/problems/0018.四数之和.md +++ b/problems/0018.四数之和.md @@ -253,7 +253,7 @@ public class Solution { for (int k = 0; k < nums.length; k++) { // 剪枝处理 if (nums[k] > target && nums[k] >= 0) { - break; + break; // 此处的break可以等价于return result; } // 对nums[k]去重 if (k > 0 && nums[k] == nums[k - 1]) { @@ -262,7 +262,7 @@ public class Solution { for (int i = k + 1; i < nums.length; i++) { // 第二级剪枝 if (nums[k] + nums[i] > target && nums[k] + nums[i] >= 0) { - break; + break; // 注意是break到上一级for循环,如果直接return result;会有遗漏 } // 对nums[i]去重 if (i > k + 1 && nums[i] == nums[i - 1]) { @@ -802,3 +802,4 @@ end + From 059b6464c0b8968bd139764988ecbc652eebdabe Mon Sep 17 00:00:00 2001 From: "Zhen (Evan) Wang" <273509239@qq.com> Date: Tue, 14 Jan 2025 10:36:56 +0800 Subject: [PATCH 15/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200102.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?--199.=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E5=8F=B3=E8=A7=86?= =?UTF-8?q?=E5=9B=BE=20=20C#=20=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0102.二叉树的层序遍历中的199.二叉树的右视图 C# 代码 --- problems/0102.二叉树的层序遍历.md | 41 +++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 98e1e98a..0c3b0f8c 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1231,6 +1231,47 @@ impl Solution { } ``` +#### C#: + +```C# 199.二叉树的右视图 +public class Solution +{ + public IList RightSideView(TreeNode root) + { + var result = new List(); + Queue queue = new(); + + if (root != null) + { + queue.Enqueue(root); + } + while (queue.Count > 0) + { + int count = queue.Count; + int lastValue = count - 1; + for (int i = 0; i < count; i++) + { + var currentNode = queue.Dequeue(); + if (i == lastValue) + { + result.Add(currentNode.val); + } + + // lastValue == i == count -1 : left 先于 right 进入Queue + if (currentNode.left != null) queue.Enqueue(currentNode.left); + if (currentNode.right != null) queue.Enqueue(currentNode.right); + + //// lastValue == i == 0: right 先于 left 进入Queue + // if(currentNode.right !=null ) queue.Enqueue(currentNode.right); + // if(currentNode.left !=null ) queue.Enqueue(currentNode.left); + } + } + + return result; + } +} +``` + ## 637.二叉树的层平均值 [力扣题目链接](https://leetcode.cn/problems/average-of-levels-in-binary-tree/) From 2bcd88a096d694b295a855062339579458c0769f Mon Sep 17 00:00:00 2001 From: "Zhen (Evan) Wang" <273509239@qq.com> Date: Tue, 14 Jan 2025 11:18:41 +0800 Subject: [PATCH 16/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200102.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?-=20637.=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=B9=B3?= =?UTF-8?q?=E5=9D=87=E5=80=BC=20C#=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 637.二叉树的层平均值 C# 版本 --- problems/0102.二叉树的层序遍历.md | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 0c3b0f8c..ce53e49a 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1599,6 +1599,35 @@ impl Solution { } ``` +#### C#: + +```C# 二叉树的层平均值 +public class Solution { + public IList AverageOfLevels(TreeNode root) { + var result= new List(); + Queue queue = new(); + if(root !=null) queue.Enqueue(root); + + while (queue.Count > 0) + { + int count = queue.Count; + double value=0; + for (int i = 0; i < count; i++) + { + var curentNode=queue.Dequeue(); + value += curentNode.val; + if (curentNode.left!=null) queue.Enqueue(curentNode.left); + if (curentNode.right!=null) queue.Enqueue(curentNode.right); + } + result.Add(value/count); + } + + return result; + } +} + +``` + ## 429.N叉树的层序遍历 [力扣题目链接](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/) From 275efa0c70ecdb36c2c77eb2e26059b7b8b29d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Wed, 15 Jan 2025 15:52:42 +0800 Subject: [PATCH 17/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A00459.=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E7=9A=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md=E7=9A=84Java?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E4=BA=8C=E5=89=8D=E7=BC=80=E8=A1=A8=E4=B8=8D?= =?UTF-8?q?=E5=87=8F=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0459.重复的子字符串.md | 42 +++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md index de0e6e4d..bdced0ef 100644 --- a/problems/0459.重复的子字符串.md +++ b/problems/0459.重复的子字符串.md @@ -390,6 +390,8 @@ public: ### Java: +(版本一) 前缀表 减一 + ```java class Solution { public boolean repeatedSubstringPattern(String s) { @@ -420,6 +422,45 @@ class Solution { } ``` +(版本二) 前缀表 不减一 + +```java +/* + * 充分条件:如果字符串s是由重复子串组成的,那么它的最长相等前后缀不包含的子串一定是s的最小重复子串。 + * 必要条件:如果字符串s的最长相等前后缀不包含的子串是s的最小重复子串,那么s必然是由重复子串组成的。 + * 推得:当字符串s的长度可以被其最长相等前后缀不包含的子串的长度整除时,不包含的子串就是s的最小重复子串。 + * + * 时间复杂度:O(n) + * 空间复杂度:O(n) + */ +class Solution { + public boolean repeatedSubstringPattern(String s) { + // if (s.equals("")) return false; + // 边界判断(可以去掉,因为题目给定范围是1 <= s.length <= 10^4) + int n = s.length(); + + // Step 1.构建KMP算法的前缀表 + int[] next = new int[n]; // 前缀表的值表示 以该位置结尾的字符串的最长相等前后缀的长度 + int j = 0; + next[0] = 0; + for (int i = 1; i < n; i++) { + while (j > 0 && s.charAt(i) != s.charAt(j)) // 只要前缀后缀还不一致,就根据前缀表回退j直到起点为止 + j = next[j - 1]; + if (s.charAt(i) == s.charAt(j)) + j++; + next[i] = j; + } + + // Step 2.判断重复子字符串 + if (next[n - 1] > 0 && n % (n - next[n - 1]) == 0) { // 当字符串s的长度可以被其最长相等前后缀不包含的子串的长度整除时 + return true; // 不包含的子串就是s的最小重复子串 + } else { + return false; + } + } +} +``` + ### Python: (版本一) 前缀表 减一 @@ -930,4 +971,3 @@ bool repeatedSubstringPattern(char* s) { - From 20385f114619bd5a1c90f0f10ebd4ed7a76b9b6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Fri, 17 Jan 2025 10:29:24 +0800 Subject: [PATCH 18/20] =?UTF-8?q?=E4=BF=AE=E6=94=B90020.=E6=9C=89=E6=95=88?= =?UTF-8?q?=E7=9A=84=E6=8B=AC=E5=8F=B7.md=E7=9A=84Java=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=B8=80=E5=A4=84=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0020.有效的括号.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index f2f5cdd1..9d9168e3 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -166,7 +166,7 @@ class Solution { deque.pop(); } } - //最后判断栈中元素是否匹配 + //遍历结束,如果栈为空,则括号全部匹配 return deque.isEmpty(); } } @@ -555,3 +555,4 @@ impl Solution { + From a66142cbc57e9f3fbe3d5b72689d5cab38fbc690 Mon Sep 17 00:00:00 2001 From: Jian Date: Fri, 17 Jan 2025 19:37:21 +0800 Subject: [PATCH 19/20] =?UTF-8?q?=E4=BF=AE=E6=94=B9239=E6=BB=91=E5=8A=A8?= =?UTF-8?q?=E7=AA=97=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BCjava=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=B3=A8=E9=87=8A=E4=B8=A5=E9=87=8D=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0239.滑动窗口最大值.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index caa24d8d..63ee3dae 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -267,7 +267,7 @@ class Solution { //利用双端队列手动实现单调队列 /** * 用一个单调队列来存储对应的下标,每当窗口滑动的时候,直接取队列的头部指针对应的值放入结果集即可 - * 单调队列类似 (tail -->) 3 --> 2 --> 1 --> 0 (--> head) (右边为头结点,元素存的是下标) + * 单调递减队列类似 (head -->) 3 --> 2 --> 1 --> 0 (--> tail) (左边为头结点,元素存的是下标) */ class Solution { public int[] maxSlidingWindow(int[] nums, int k) { @@ -281,7 +281,7 @@ class Solution { while(!deque.isEmpty() && deque.peek() < i - k + 1){ deque.poll(); } - // 2.既然是单调,就要保证每次放进去的数字要比末尾的都大,否则也弹出 + // 2.维护单调递减队列:新元素若大于队尾元素,则弹出队尾元素,直到满足单调性 while(!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) { deque.pollLast(); } @@ -894,3 +894,4 @@ public: + From b764a124b71350bd8d008cddaddae3136f84c5f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Mon, 20 Jan 2025 22:59:33 +0800 Subject: [PATCH 20/20] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95?= =?UTF-8?q?.md=E7=9A=84Java=E7=89=88=E6=9C=AC=E6=B3=A8=E9=87=8A=E7=AC=94?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的统一迭代法.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/problems/二叉树的统一迭代法.md b/problems/二叉树的统一迭代法.md index 037cf110..a6d4e3ff 100644 --- a/problems/二叉树的统一迭代法.md +++ b/problems/二叉树的统一迭代法.md @@ -238,7 +238,7 @@ class Solution { while (!st.empty()) { TreeNode node = st.peek(); if (node != null) { - st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中 + st.pop(); // 将该节点弹出,避免重复操作,下面再将右左中节点添加到栈中(前序遍历-中左右,入栈顺序右左中) if (node.right!=null) st.push(node.right); // 添加右节点(空节点不入栈) if (node.left!=null) st.push(node.left); // 添加左节点(空节点不入栈) st.push(node); // 添加中节点 @@ -266,11 +266,10 @@ public List inorderTraversal(TreeNode root) { while (!st.empty()) { TreeNode node = st.peek(); if (node != null) { - st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中 + st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中(中序遍历-左中右,入栈顺序右中左) if (node.right!=null) st.push(node.right); // 添加右节点(空节点不入栈) st.push(node); // 添加中节点 st.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。 - if (node.left!=null) st.push(node.left); // 添加左节点(空节点不入栈) } else { // 只有遇到空节点的时候,才将下一个节点放进结果集 st.pop(); // 将空节点弹出 @@ -294,7 +293,7 @@ class Solution { while (!st.empty()) { TreeNode node = st.peek(); if (node != null) { - st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中 + st.pop(); // 将该节点弹出,避免重复操作,下面再将中右左节点添加到栈中(后序遍历-左右中,入栈顺序中右左) st.push(node); // 添加中节点 st.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。 if (node.right!=null) st.push(node.right); // 添加右节点(空节点不入栈) @@ -975,3 +974,4 @@ public IList PostorderTraversal(TreeNode root) +