Skip to content

Commit 404f916

Browse files
committed
update: 添加问题“1582.二进制矩阵中的特殊位置”的代码(并更新其题解) (#1428)
1582: AC.cpp (#1426) - AC,100.00%,95.00% Signed-off-by: LetMeFly666 <Tisfy@qq.com>
1 parent 5fc7b7a commit 404f916

5 files changed

Lines changed: 102 additions & 6 deletions

.commitmsg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1582: AC.cpp (#1426) - AC,100.00%,95.00%
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-03-04 23:58:55
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-03-05 00:02:09
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int numSpecial(vector<vector<int>>& mat) {
14+
int ans = 0;
15+
int n = mat.size(), m = mat[0].size();
16+
for (int i = 0; i < n; i++) {
17+
bool only1 = true;
18+
int idx = -1;
19+
for (int j = 0; j < m; j++) {
20+
if (mat[i][j]) {
21+
if (idx != -1) {
22+
only1 = false;
23+
break;
24+
}
25+
idx = j;
26+
}
27+
}
28+
if (!only1 || idx == -1) {
29+
continue;
30+
}
31+
for (int k = 0; k < n; k++) {
32+
if (mat[k][idx] && k != i) {
33+
only1 = false;
34+
break;
35+
}
36+
}
37+
ans += only1;
38+
}
39+
return ans;
40+
}
41+
};

Solutions/LeetCode 1582.二进制矩阵中的特殊位置.md

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
2-
title: 1582.二进制矩阵中的特殊位置
2+
title: 1582.二进制矩阵中的特殊位置:模拟/记录每行每列1个数/行有单1再看列
33
date: 2022-09-04 14:59:22
44
tags: [题解, LeetCode, 简单, 数组, 矩阵]
55
categories: [题解, LeetCode]
66
---
77

8-
# 【LetMeFly】1582.二进制矩阵中的特殊位置
8+
# 【LetMeFly】1582.二进制矩阵中的特殊位置:模拟/记录每行每列1个数/行有单1再看列
99

1010
力扣题目链接:[https://leetcode.cn/problems/special-positions-in-a-binary-matrix/](https://leetcode.cn/problems/special-positions-in-a-binary-matrix/)
1111

@@ -63,8 +63,6 @@ categories: [题解, LeetCode]
6363
<li><code>mat[i][j]</code> 是 <code>0</code> 或 <code>1</code></li>
6464
</ul>
6565

66-
67-
6866
## 方法一:直接模拟
6967

7068
直接遍历一遍原始矩阵,如果当前元素是1,就判断是否这一行除此元素外都是0并且这一列除此元素外都是0。
@@ -147,5 +145,53 @@ public:
147145
};
148146
```
149147

148+
## 方法三:行有单1再看列
149+
150+
从第一行到最后一行遍历,对于每一行,若这一行只有一个1,则遍历1这一列,若这一列其他位置皆为0,则此1为特殊位置。
151+
152+
+ 时间复杂度$O(n(n+m))$,其中原始矩阵的大小为$n\times m$
153+
+ 空间复杂度$O(1)$
154+
155+
### AC代码
156+
157+
#### C++
158+
159+
```cpp
160+
/*
161+
* @LastEditTime: 2026-03-05 00:02:09
162+
*/
163+
class Solution {
164+
public:
165+
int numSpecial(vector<vector<int>>& mat) {
166+
int ans = 0;
167+
int n = mat.size(), m = mat[0].size();
168+
for (int i = 0; i < n; i++) {
169+
bool only1 = true;
170+
int idx = -1;
171+
for (int j = 0; j < m; j++) {
172+
if (mat[i][j]) {
173+
if (idx != -1) {
174+
only1 = false;
175+
break;
176+
}
177+
idx = j;
178+
}
179+
}
180+
if (!only1 || idx == -1) {
181+
continue;
182+
}
183+
for (int k = 0; k < n; k++) {
184+
if (mat[k][idx] && k != i) {
185+
only1 = false;
186+
break;
187+
}
188+
}
189+
ans += only1;
190+
}
191+
return ans;
192+
}
193+
};
194+
```
195+
150196
> 同步发文于CSDN,原创不易,转载请附上[原文链接](https://blog.letmefly.xyz/2022/09/04/LeetCode%201582.%E4%BA%8C%E8%BF%9B%E5%88%B6%E7%9F%A9%E9%98%B5%E4%B8%AD%E7%9A%84%E7%89%B9%E6%AE%8A%E4%BD%8D%E7%BD%AE/)哦~
151197
> Tisfy:[https://letmefly.blog.csdn.net/article/details/126689744](https://letmefly.blog.csdn.net/article/details/126689744)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,14 @@ categories: [自用]
18481848
|treason|n. 危害国家罪,叛逆,通敌,背叛|
18491849
|||
18501850
|feeble|adj. 虚弱的,无效的|
1851+
|||
1852+
|gymnast|n. 体操运动员|
1853+
|||
1854+
|backbone|n. 脊椎,骨干,支柱|
1855+
|constable|n. 警员<br/>头衔时首字母大写,如Constable Smith|
1856+
|peninsular|adj. 半岛的|
1857+
|||
1858+
|wrath|n. 怒火|
18511859

18521860
+ 这个web要是能设计得可以闭眼(完全不睁眼)键盘控制背单词就好了。
18531861
+ 也许可以加个AI用最近词编故事功能(返回接口中支持标注所使用单词高亮?)

toSay.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
* @Author: LetMeFly
33
* @Date: 2026-03-03 20:26:53
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2026-03-03 20:27:07
5+
* @LastEditTime: 2026-03-03 20:41:15
66
-->
7-
血月!狼人,嗷呜~
7+
昨天连CSDN发文都变得需要审核好久了 (不知道是不是巧合

0 commit comments

Comments
 (0)