Skip to content

Commit ede8673

Browse files
committed
update: 添加问题“3546.等和矩阵分割I”的代码和题解 (#1464)
2906: AC.cpp ($1463) - AC,92.86%,83.33% 中断了一阵子,其实写不了这么久 Signed-off-by: LetMeFly666 <Tisfy@qq.com>
1 parent 5448915 commit ede8673

7 files changed

Lines changed: 197 additions & 1 deletion

.commitmsg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2906: AC.cpp ($1463) - AC,92.86%,83.33%
2+
3+
中断了一阵子,其实写不了这么久
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-03-25 21:42:32
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-03-25 22:05:04
6+
*/
7+
#ifdef _DEBUG
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
#define CHECK_AND_BREAK(now, sum) \
12+
if ((now) == (sum)) { \
13+
return true; \
14+
} \
15+
if ((now) > (sum)) { \
16+
break; \
17+
}
18+
typedef long long ll;
19+
class Solution {
20+
public:
21+
bool canPartitionGrid(vector<vector<int>>& grid) {
22+
ll sum = 0;
23+
for (vector<int>& row : grid) {
24+
for (int& v : row) {
25+
sum += v;
26+
}
27+
}
28+
if (sum % 2) {
29+
return false;
30+
}
31+
sum /= 2;
32+
33+
ll now = 0;
34+
for (vector<int>& row : grid) {
35+
for (int& v : row) {
36+
now += v;
37+
}
38+
CHECK_AND_BREAK(now, sum)
39+
}
40+
41+
now = 0;
42+
for (int j = 0; j < grid[0].size(); j++) {
43+
for (int i = 0; i < grid.size(); i++) {
44+
now += grid[i][j];
45+
}
46+
CHECK_AND_BREAK(now, sum)
47+
}
48+
49+
return false;
50+
}
51+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,7 @@
11261126
|3516.找到最近的人|简单|<a href="https://leetcode.cn/problems/find-closest-person/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/09/04/LeetCode%203516.%E6%89%BE%E5%88%B0%E6%9C%80%E8%BF%91%E7%9A%84%E4%BA%BA/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151184074" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-closest-person/solutions/3772009/letmefly-3516zhao-dao-zui-jin-de-ren-ji-8rlbz/" target="_blank">LeetCode题解</a>|
11271127
|3531.统计被覆盖的建筑|中等|<a href="https://leetcode.cn/problems/count-covered-buildings/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/11/LeetCode%203531.%E7%BB%9F%E8%AE%A1%E8%A2%AB%E8%A6%86%E7%9B%96%E7%9A%84%E5%BB%BA%E7%AD%91/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/155824933" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-covered-buildings/solutions/3854864/letmefly-3531tong-ji-bei-fu-gai-de-jian-9kgng/" target="_blank">LeetCode题解</a>|
11281128
|3541.找到频率最高的元音和辅音|简单|<a href="https://leetcode.cn/problems/find-most-frequent-vowel-and-consonant/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/09/13/LeetCode%203541.%E6%89%BE%E5%88%B0%E9%A2%91%E7%8E%87%E6%9C%80%E9%AB%98%E7%9A%84%E5%85%83%E9%9F%B3%E5%92%8C%E8%BE%85%E9%9F%B3/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151653999" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-most-frequent-vowel-and-consonant/solutions/3780744/letmefly-3541zhao-dao-pin-lu-zui-gao-de-yv5vs/" target="_blank">LeetCode题解</a>|
1129+
|3546.等和矩阵分割I|中等|<a href="https://leetcode.cn/problems/equal-sum-grid-partition-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/03/25/LeetCode%203546.%E7%AD%89%E5%92%8C%E7%9F%A9%E9%98%B5%E5%88%86%E5%89%B2I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/159476515" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/equal-sum-grid-partition-i/solutions/3937005/letmefly-3546deng-he-ju-zhen-fen-ge-iji-0n0ee/" target="_blank">LeetCode题解</a>|
11291130
|3567.子矩阵的最小绝对差|中等|<a href="https://leetcode.cn/problems/minimum-absolute-difference-in-sliding-submatrix/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/03/20/LeetCode%203567.%E5%AD%90%E7%9F%A9%E9%98%B5%E7%9A%84%E6%9C%80%E5%B0%8F%E7%BB%9D%E5%AF%B9%E5%B7%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/159291000" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-absolute-difference-in-sliding-submatrix/solutions/3932222/letmefly-3567zi-ju-zhen-de-zui-xiao-jue-4ux0g/" target="_blank">LeetCode题解</a>|
11301131
|3573.买卖股票的最佳时机V|中等|<a href="https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-v/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/17/LeetCode%203573.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BAV/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/156029259" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-v/solutions/3859674/letmefly-3573mai-mai-gu-piao-de-zui-jia-592yz/" target="_blank">LeetCode题解</a>|
11311132
|3577.统计计算机解锁顺序排列数|中等|<a href="https://leetcode.cn/problems/count-the-number-of-computer-unlocking-permutations/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/10/LeetCode%203577.%E7%BB%9F%E8%AE%A1%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%A3%E9%94%81%E9%A1%BA%E5%BA%8F%E6%8E%92%E5%88%97%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/155791805" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-the-number-of-computer-unlocking-permutations/solutions/3854206/letmefly-3577tong-ji-ji-suan-ji-jie-suo-5b6b8/" target="_blank">LeetCode题解</a>|

Solutions/LeetCode 2906.构造乘积矩阵.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ p[0][2] = grid[0][0] * grid[0][1] = 12345 * 2 = 24690. 24690 % 12345 = 0 ,所
5555

5656
## 解题方法一:前后缀分解 O(mn)空间
5757

58-
由于模数并非质数,所以不方便全部求积后通过模逆元做除法,因此可以使用前缀和和后缀和两个数组,分别记录每个位置前面元素的乘积以及后面元素的乘积。
58+
由于模数$12345$并非质数,所以不方便全部求积后通过模逆元做除法,因此可以使用前缀和和后缀和两个数组,分别记录每个位置前面元素的乘积以及后面元素的乘积。
5959

6060
二维数组怎么定义“前面的元素”和“后面的元素”呢?很简单,把$n$行$m$列的二维数组看成$n\times m$大小的一维数组就好了,一维数组的下标$t$在原始二维数组中的下标是$(\lfloor\frac{t}{m}\rfloor, t\ \%\ m)$。
6161

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
---
2+
title: 3546.等和矩阵分割 I:记总记当前(遍历)
3+
date: 2026-03-25 22:08:15
4+
tags: [题解, LeetCode, 中等, 数组, 枚举, 矩阵, 前缀和, 遍历]
5+
categories: [题解, LeetCode]
6+
index_img: https://pic.leetcode.cn/1746839596-kWigaF-lc.jpeg
7+
---
8+
9+
# 【LetMeFly】3546.等和矩阵分割 I:记总记当前(遍历)
10+
11+
力扣题目链接:[https://leetcode.cn/problems/equal-sum-grid-partition-i/](https://leetcode.cn/problems/equal-sum-grid-partition-i/)
12+
13+
<p>给你一个由正整数组成的 <code>m x n</code> 矩阵 <code>grid</code>。你的任务是判断是否可以通过&nbsp;<strong>一条水平或一条垂直分割线&nbsp;</strong>将矩阵分割成两部分,使得:</p>
14+
15+
<ul>
16+
<li>分割后形成的每个部分都是&nbsp;<strong>非空&nbsp;</strong>的。</li>
17+
<li>两个部分中所有元素的和&nbsp;<strong>相等&nbsp;</strong>。</li>
18+
</ul>
19+
20+
<p>如果存在这样的分割,返回 <code>true</code>;否则,返回 <code>false</code>。</p>
21+
22+
<p>&nbsp;</p>
23+
24+
<p><strong class="example">示例 1:</strong></p>
25+
26+
<div class="example-block">
27+
<p><strong>输入:</strong> grid = [[1,4],[2,3]]</p>
28+
29+
<p><strong>输出:</strong> true</p>
30+
31+
<p><strong>解释:</strong></p>
32+
33+
<p><img alt="" src="https://pic.leetcode.cn/1746839596-kWigaF-lc.jpeg" style="height: 200px; width: 200px;" /></p>
34+
35+
<p>在第 0 行和第 1 行之间进行水平分割,得到两个非空部分,每部分的元素之和为 5。因此,答案是 <code>true</code>。</p>
36+
</div>
37+
38+
<p><strong class="example">示例 2:</strong></p>
39+
40+
<div class="example-block">
41+
<p><strong>输入:</strong> grid = [[1,3],[2,4]]</p>
42+
43+
<p><strong>输出:</strong> false</p>
44+
45+
<p><strong>解释:</strong></p>
46+
47+
<p>无论是水平分割还是垂直分割,都无法使两个非空部分的元素之和相等。因此,答案是 <code>false</code>。</p>
48+
</div>
49+
50+
<p>&nbsp;</p>
51+
52+
<p><strong>提示:</strong></p>
53+
54+
<ul>
55+
<li><code>1 &lt;= m == grid.length &lt;= 10<sup>5</sup></code></li>
56+
<li><code>1 &lt;= n == grid[i].length &lt;= 10<sup>5</sup></code></li>
57+
<li><code>2 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
58+
<li><code>1 &lt;= grid[i][j] &lt;= 10<sup>5</sup></code></li>
59+
</ul>
60+
61+
<p>&nbsp;</p>
62+
63+
64+
65+
## 解题方法:三次遍历
66+
67+
第一次遍历计算整个grid的元素之和,若非偶数直接返回`false`,若为偶数则直接除以$2$。
68+
69+
接着从上往下遍历grid,每次累加一整行的元素,若恰好等于除以二后的总和则返回true,大于则break。
70+
71+
最后从左往右遍历,方法和从上往下遍历同理。
72+
73+
+ 时间复杂度$O(nm)$
74+
+ 空间复杂度$O(1)$
75+
76+
### AC代码
77+
78+
#### C++
79+
80+
```cpp
81+
/*
82+
* @LastEditTime: 2026-03-25 22:05:04
83+
*/
84+
#define CHECK_AND_BREAK(now, sum) \
85+
if ((now) == (sum)) { \
86+
return true; \
87+
} \
88+
if ((now) > (sum)) { \
89+
break; \
90+
}
91+
typedef long long ll;
92+
class Solution {
93+
public:
94+
bool canPartitionGrid(vector<vector<int>>& grid) {
95+
ll sum = 0;
96+
for (vector<int>& row : grid) {
97+
for (int& v : row) {
98+
sum += v;
99+
}
100+
}
101+
if (sum % 2) {
102+
return false;
103+
}
104+
sum /= 2;
105+
106+
ll now = 0;
107+
for (vector<int>& row : grid) {
108+
for (int& v : row) {
109+
now += v;
110+
}
111+
CHECK_AND_BREAK(now, sum)
112+
}
113+
114+
now = 0;
115+
for (int j = 0; j < grid[0].size(); j++) {
116+
for (int i = 0; i < grid.size(); i++) {
117+
now += grid[i][j];
118+
}
119+
CHECK_AND_BREAK(now, sum)
120+
}
121+
122+
return false;
123+
}
124+
};
125+
126+
```
127+
128+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/159476515)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/03/25/LeetCode%203546.%E7%AD%89%E5%92%8C%E7%9F%A9%E9%98%B5%E5%88%86%E5%89%B2I/)~
129+
>
130+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

chat.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
###
2+
# @Author: LetMeFly
3+
# @Date: 2026-03-24 23:16:39
4+
# @LastEditors: LetMeFly.xyz
5+
# @LastEditTime: 2026-03-24 23:18:55
6+
###
7+
我linux想默认4534端口ssh登录,而22端口使用一个蜜罐程序监听
8+
这个蜜罐程序在用户尝试连接时不论输入什么密码都会连接成功,然后提示一句Welcome,you idot,之后是一个假shell。
9+
我想让这个shell尽可能真实,你帮我设计一下。
10+
11+
使用go实现,尽量不要使用第三方依赖。
File renamed without changes.

0 commit comments

Comments
 (0)