Skip to content

Commit fd1008f

Browse files
committed
update: 添加问题“3212.统计X和Y频数相等的子矩阵数量”的代码和题解 (#1452)
3212: AC.cpp (#1451) - AC,15.57%,85.38% + (en) Signed-off-by: LetMeFly666 <Tisfy@qq.com>
1 parent 907d670 commit fd1008f

4 files changed

Lines changed: 168 additions & 1 deletion

File tree

.commitmsg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3070: AC.cpp (#1449) - AC,91.79%,100.00% + (en)
1+
3212: AC.cpp (#1451) - AC,15.57%,85.38% + (en)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-03-19 22:10:19
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-03-19 22:19:31
6+
*/
7+
#ifdef _DEBUG
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
inline int v(int x) {
14+
return x == 'X' ? 1 : x == 'Y' ? -1 : 0;
15+
}
16+
public:
17+
int numberOfSubmatrices(vector<vector<char>>& grid) {
18+
int ans = 0;
19+
int n = grid.size(), m = grid[0].size();
20+
vector<vector<bool>> hasX(n, vector<bool>(m)); // has x or y
21+
vector<vector<int>> diff(n, vector<int>(m));
22+
for (int i = 0; i < n; i++) {
23+
for (int j = 0; j < m; j++) {
24+
if (i && j) {
25+
hasX[i][j] = hasX[i - 1][j] | hasX[i][j - 1] | (grid[i][j] != '.');
26+
diff[i][j] = diff[i - 1][j] + diff[i][j - 1] - diff[i - 1][j - 1] + v(grid[i][j]);
27+
} else if (i) {
28+
hasX[i][j] = hasX[i - 1][j] | (grid[i][j] != '.');
29+
diff[i][j] = diff[i - 1][j] + v(grid[i][j]);
30+
} else if (j) {
31+
hasX[i][j] = hasX[i][j - 1] | (grid[i][j] != '.');
32+
diff[i][j] = diff[i][j - 1] + v(grid[i][j]);
33+
} else {
34+
hasX[i][j] = grid[i][j] != '.';
35+
diff[i][j] = v(grid[i][j]);
36+
}
37+
ans += (!diff[i][j] && hasX[i][j]);
38+
}
39+
}
40+
return ans;
41+
}
42+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,7 @@
10601060
|3206.交替组I|简单|<a href="https://leetcode.cn/problems/alternating-groups-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/26/LeetCode%203206.%E4%BA%A4%E6%9B%BF%E7%BB%84I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144071026" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/alternating-groups-i/solutions/3001910/letmefly-3206jiao-ti-zu-ibian-li-by-tisf-euqx/" target="_blank">LeetCode题解</a>|
10611061
|3208.交替组II|中等|<a href="https://leetcode.cn/problems/alternating-groups-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/28/LeetCode%203208.%E4%BA%A4%E6%9B%BF%E7%BB%84II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144123453" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/alternating-groups-ii/solutions/3004235/letmefly-3208jiao-ti-zu-iihua-dong-chuan-5bcc/" target="_blank">LeetCode题解</a>|
10621062
|3211.生成不含相邻零的二进制字符串|中等|<a href="https://leetcode.cn/problems/generate-binary-strings-without-adjacent-zeros/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/29/LeetCode%203211.%E7%94%9F%E6%88%90%E4%B8%8D%E5%90%AB%E7%9B%B8%E9%82%BB%E9%9B%B6%E7%9A%84%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143352841" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/generate-binary-strings-without-adjacent-zeros/solutions/2970587/letmefly-3211sheng-cheng-bu-han-xiang-li-sdh3/" target="_blank">LeetCode题解</a>|
1063+
|3212.统计X和Y频数相等的子矩阵数量|中等|<a href="https://leetcode.cn/problems/count-submatrices-with-equal-frequency-of-x-and-y/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/03/19/LeetCode%203212.%E7%BB%9F%E8%AE%A1X%E5%92%8CY%E9%A2%91%E6%95%B0%E7%9B%B8%E7%AD%89%E7%9A%84%E5%AD%90%E7%9F%A9%E9%98%B5%E6%95%B0%E9%87%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/159253457" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-submatrices-with-equal-frequency-of-x-and-y/solutions/3931242/letmefly-3212tong-ji-x-he-y-pin-shu-xian-x2ai/" target="_blank">LeetCode题解</a>|
10631064
|3216.交换后字典序最小的字符串|简单|<a href="https://leetcode.cn/problems/lexicographically-smallest-string-after-a-swap/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/30/LeetCode%203216.%E4%BA%A4%E6%8D%A2%E5%90%8E%E5%AD%97%E5%85%B8%E5%BA%8F%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143362223" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/lexicographically-smallest-string-after-a-swap/solutions/2971048/letmefly-3216jiao-huan-hou-zi-dian-xu-zu-nxp9/" target="_blank">LeetCode题解</a>|
10641065
|3217.从链表中移除在数组中存在的节点|中等|<a href="https://leetcode.cn/problems/delete-nodes-from-linked-list-present-in-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/11/01/LeetCode%203217.%E4%BB%8E%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%A7%BB%E9%99%A4%E5%9C%A8%E6%95%B0%E7%BB%84%E4%B8%AD%E5%AD%98%E5%9C%A8%E7%9A%84%E8%8A%82%E7%82%B9/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/154259704" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/delete-nodes-from-linked-list-present-in-array/solutions/3821125/letmefly-3217cong-lian-biao-zhong-yi-chu-2mcy/" target="_blank">LeetCode题解</a>|
10651066
|3218.切蛋糕的最小总开销I|中等|<a href="https://leetcode.cn/problems/minimum-cost-for-cutting-cake-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/25/LeetCode%203218.%E5%88%87%E8%9B%8B%E7%B3%95%E7%9A%84%E6%9C%80%E5%B0%8F%E6%80%BB%E5%BC%80%E9%94%80I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144728332" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-cost-for-cutting-cake-i/solutions/3030374/letmefly-3218qie-dan-gao-de-zui-xiao-zon-a1t6/" target="_blank">LeetCode题解</a>|
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
---
2+
title: 3212.统计 X 和 Y 频数相等的子矩阵数量:前缀和
3+
date: 2026-03-19 22:20:10
4+
tags: [题解, LeetCode, 中等, 数组, 矩阵, 前缀和]
5+
categories: [题解, LeetCode]
6+
index_img: https://assets.leetcode.com/uploads/2024/06/07/examplems.png
7+
---
8+
9+
# 【LetMeFly】3212.统计 X 和 Y 频数相等的子矩阵数量:前缀和
10+
11+
力扣题目链接:[https://leetcode.cn/problems/count-submatrices-with-equal-frequency-of-x-and-y/](https://leetcode.cn/problems/count-submatrices-with-equal-frequency-of-x-and-y/)
12+
13+
<p>给你一个二维字符矩阵 <code>grid</code>,其中 <code>grid[i][j]</code> 可能是 <code>'X'</code>、<code>'Y'</code> 或 <code>'.'</code>,返回满足以下条件的<span data-keyword="submatrix">子矩阵</span>数量:</p>
14+
15+
<ul>
16+
<li>包含 <code>grid[0][0]</code></li>
17+
<li><code>'X'</code> 和 <code>'Y'</code> 的频数相等。</li>
18+
<li>至少包含一个 <code>'X'</code>。</li>
19+
</ul>
20+
21+
<p>&nbsp;</p>
22+
23+
<p><strong class="example">示例 1:</strong></p>
24+
25+
<div class="example-block">
26+
<p><strong>输入:</strong> <span class="example-io">grid = [["X","Y","."],["Y",".","."]]</span></p>
27+
28+
<p><strong>输出:</strong> <span class="example-io">3</span></p>
29+
30+
<p><strong>解释:</strong></p>
31+
32+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2024/06/07/examplems.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 175px; height: 350px;" /></strong></p>
33+
</div>
34+
35+
<p><strong class="example">示例 2:</strong></p>
36+
37+
<div class="example-block">
38+
<p><strong>输入:</strong> <span class="example-io">grid = [["X","X"],["X","Y"]]</span></p>
39+
40+
<p><strong>输出:</strong> <span class="example-io">0</span></p>
41+
42+
<p><strong>解释:</strong></p>
43+
44+
<p>不存在满足 <code>'X'</code> 和 <code>'Y'</code> 频数相等的子矩阵。</p>
45+
</div>
46+
47+
<p><strong class="example">示例 3:</strong></p>
48+
49+
<div class="example-block">
50+
<p><strong>输入:</strong> <span class="example-io">grid = [[".","."],[".","."]]</span></p>
51+
52+
<p><strong>输出:</strong> <span class="example-io">0</span></p>
53+
54+
<p><strong>解释:</strong></p>
55+
56+
<p>不存在满足至少包含一个 <code>'X'</code> 的子矩阵。</p>
57+
</div>
58+
59+
<p>&nbsp;</p>
60+
61+
<p><strong>提示:</strong></p>
62+
63+
<ul>
64+
<li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li>
65+
<li><code>grid[i][j]</code> 可能是 <code>'X'</code>、<code>'Y'</code> 或 <code>'.'</code>.</li>
66+
</ul>
67+
68+
69+
> 前置注意事项:本题的子矩阵是必须包含左上角的矩阵。
70+
71+
## 解题方法:前缀和
72+
73+
类似昨天的每日一题[3070.元素和小于等于 k 的子矩阵的数目](https://blog.letmefly.xyz/2026/03/18/LeetCode%203070.%E5%85%83%E7%B4%A0%E5%92%8C%E5%B0%8F%E4%BA%8E%E7%AD%89%E4%BA%8Ek%E7%9A%84%E5%AD%90%E7%9F%A9%E9%98%B5%E7%9A%84%E6%95%B0%E7%9B%AE/),使用一个$diff$数组存储从$(0,0)$到$(i,j)$的`X - Y`的数量,再额外使用一个数组存储从$(0,0)$到$(i,j)$是否存在`X`(或`Y`)。
74+
75+
遍历过程中维护并更新这两个数组即可。
76+
77+
+ 时间复杂度$O(mn)$
78+
+ 空间复杂度$O(mn)$
79+
80+
### AC代码
81+
82+
#### C++
83+
84+
```cpp
85+
/*
86+
* @LastEditTime: 2026-03-19 22:19:31
87+
*/
88+
class Solution {
89+
private:
90+
inline int v(int x) {
91+
return x == 'X' ? 1 : x == 'Y' ? -1 : 0;
92+
}
93+
public:
94+
int numberOfSubmatrices(vector<vector<char>>& grid) {
95+
int ans = 0;
96+
int n = grid.size(), m = grid[0].size();
97+
vector<vector<bool>> hasX(n, vector<bool>(m)); // has x or y
98+
vector<vector<int>> diff(n, vector<int>(m));
99+
for (int i = 0; i < n; i++) {
100+
for (int j = 0; j < m; j++) {
101+
if (i && j) {
102+
hasX[i][j] = hasX[i - 1][j] | hasX[i][j - 1] | (grid[i][j] != '.');
103+
diff[i][j] = diff[i - 1][j] + diff[i][j - 1] - diff[i - 1][j - 1] + v(grid[i][j]);
104+
} else if (i) {
105+
hasX[i][j] = hasX[i - 1][j] | (grid[i][j] != '.');
106+
diff[i][j] = diff[i - 1][j] + v(grid[i][j]);
107+
} else if (j) {
108+
hasX[i][j] = hasX[i][j - 1] | (grid[i][j] != '.');
109+
diff[i][j] = diff[i][j - 1] + v(grid[i][j]);
110+
} else {
111+
hasX[i][j] = grid[i][j] != '.';
112+
diff[i][j] = v(grid[i][j]);
113+
}
114+
ans += (!diff[i][j] && hasX[i][j]);
115+
}
116+
}
117+
return ans;
118+
}
119+
};
120+
```
121+
122+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/159253457)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/03/19/LeetCode%203212.%E7%BB%9F%E8%AE%A1X%E5%92%8CY%E9%A2%91%E6%95%B0%E7%9B%B8%E7%AD%89%E7%9A%84%E5%AD%90%E7%9F%A9%E9%98%B5%E6%95%B0%E9%87%8F/)哦~
123+
>
124+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

0 commit comments

Comments
 (0)