Skip to content

Commit 70d3537

Browse files
committed
feat: add solutions to lc problem: No.1582
No.1582.Special Positions in a Binary Matrix
1 parent 6e6f07f commit 70d3537

File tree

5 files changed

+258
-0
lines changed

5 files changed

+258
-0
lines changed

solution/1500-1599/1582.Special Positions in a Binary Matrix/README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,97 @@ func numSpecial(mat [][]int) int {
176176
}
177177
```
178178

179+
### **C**
180+
181+
```c
182+
int numSpecial(int **mat, int matSize, int *matColSize) {
183+
int m = matSize;
184+
int n = *matColSize;
185+
int *rows = (int *) malloc(sizeof(int) * m);
186+
int *cols = (int *) malloc(sizeof(int) * n);
187+
memset(rows, 0, sizeof(int) * m);
188+
memset(cols, 0, sizeof(int) * n);
189+
for (int i = 0; i < m; i++) {
190+
for (int j = 0; j < n; j++) {
191+
if (mat[i][j] == 1) {
192+
rows[i]++;
193+
cols[j]++;
194+
}
195+
}
196+
}
197+
int res = 0;
198+
for (int i = 0; i < m; i++) {
199+
for (int j = 0; j < n; j++) {
200+
if (mat[i][j] == 1 && rows[i] == 1 && cols[j] == 1) {
201+
res++;
202+
}
203+
}
204+
}
205+
free(rows);
206+
free(cols);
207+
return res;
208+
}
209+
```
210+
211+
### **TypeScript**
212+
213+
```ts
214+
function numSpecial(mat: number[][]): number {
215+
const m = mat.length;
216+
const n = mat[0].length;
217+
const rows = new Array(m).fill(0);
218+
const cols = new Array(n).fill(0);
219+
for (let i = 0; i < m; i++) {
220+
for (let j = 0; j < n; j++) {
221+
if (mat[i][j] === 1) {
222+
rows[i]++;
223+
cols[j]++;
224+
}
225+
}
226+
}
227+
228+
let res = 0;
229+
for (let i = 0; i < m; i++) {
230+
for (let j = 0; j < n; j++) {
231+
if (mat[i][j] === 1 && rows[i] === 1 && cols[j] === 1) {
232+
res++;
233+
}
234+
}
235+
}
236+
237+
return res;
238+
}
239+
```
240+
241+
### **Rust**
242+
243+
```rust
244+
impl Solution {
245+
pub fn num_special(mat: Vec<Vec<i32>>) -> i32 {
246+
let m = mat.len();
247+
let n = mat[0].len();
248+
let mut rows = vec![0; m];
249+
let mut cols = vec![0; n];
250+
for i in 0..m {
251+
for j in 0..n {
252+
rows[i] += mat[i][j];
253+
cols[j] += mat[i][j];
254+
}
255+
}
256+
257+
let mut res = 0;
258+
for i in 0..m {
259+
for j in 0..n {
260+
if mat[i][j] == 1 && rows[i] == 1 && cols[j] == 1 {
261+
res += 1;
262+
}
263+
}
264+
}
265+
res
266+
}
267+
}
268+
```
269+
179270
### **...**
180271

181272
```

solution/1500-1599/1582.Special Positions in a Binary Matrix/README_EN.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,97 @@ func numSpecial(mat [][]int) int {
137137
}
138138
```
139139

140+
### **C**
141+
142+
```c
143+
int numSpecial(int **mat, int matSize, int *matColSize) {
144+
int m = matSize;
145+
int n = *matColSize;
146+
int *rows = (int *) malloc(sizeof(int) * m);
147+
int *cols = (int *) malloc(sizeof(int) * n);
148+
memset(rows, 0, sizeof(int) * m);
149+
memset(cols, 0, sizeof(int) * n);
150+
for (int i = 0; i < m; i++) {
151+
for (int j = 0; j < n; j++) {
152+
if (mat[i][j] == 1) {
153+
rows[i]++;
154+
cols[j]++;
155+
}
156+
}
157+
}
158+
int res = 0;
159+
for (int i = 0; i < m; i++) {
160+
for (int j = 0; j < n; j++) {
161+
if (mat[i][j] == 1 && rows[i] == 1 && cols[j] == 1) {
162+
res++;
163+
}
164+
}
165+
}
166+
free(rows);
167+
free(cols);
168+
return res;
169+
}
170+
```
171+
172+
### **TypeScript**
173+
174+
```ts
175+
function numSpecial(mat: number[][]): number {
176+
const m = mat.length;
177+
const n = mat[0].length;
178+
const rows = new Array(m).fill(0);
179+
const cols = new Array(n).fill(0);
180+
for (let i = 0; i < m; i++) {
181+
for (let j = 0; j < n; j++) {
182+
if (mat[i][j] === 1) {
183+
rows[i]++;
184+
cols[j]++;
185+
}
186+
}
187+
}
188+
189+
let res = 0;
190+
for (let i = 0; i < m; i++) {
191+
for (let j = 0; j < n; j++) {
192+
if (mat[i][j] === 1 && rows[i] === 1 && cols[j] === 1) {
193+
res++;
194+
}
195+
}
196+
}
197+
198+
return res;
199+
}
200+
```
201+
202+
### **Rust**
203+
204+
```rust
205+
impl Solution {
206+
pub fn num_special(mat: Vec<Vec<i32>>) -> i32 {
207+
let m = mat.len();
208+
let n = mat[0].len();
209+
let mut rows = vec![0; m];
210+
let mut cols = vec![0; n];
211+
for i in 0..m {
212+
for j in 0..n {
213+
rows[i] += mat[i][j];
214+
cols[j] += mat[i][j];
215+
}
216+
}
217+
218+
let mut res = 0;
219+
for i in 0..m {
220+
for j in 0..n {
221+
if mat[i][j] == 1 && rows[i] == 1 && cols[j] == 1 {
222+
res += 1;
223+
}
224+
}
225+
}
226+
res
227+
}
228+
}
229+
```
230+
140231
### **...**
141232

142233
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
int numSpecial(int **mat, int matSize, int *matColSize) {
2+
int m = matSize;
3+
int n = *matColSize;
4+
int *rows = (int *) malloc(sizeof(int) * m);
5+
int *cols = (int *) malloc(sizeof(int) * n);
6+
memset(rows, 0, sizeof(int) * m);
7+
memset(cols, 0, sizeof(int) * n);
8+
for (int i = 0; i < m; i++) {
9+
for (int j = 0; j < n; j++) {
10+
if (mat[i][j] == 1) {
11+
rows[i]++;
12+
cols[j]++;
13+
}
14+
}
15+
}
16+
int res = 0;
17+
for (int i = 0; i < m; i++) {
18+
for (int j = 0; j < n; j++) {
19+
if (mat[i][j] == 1 && rows[i] == 1 && cols[j] == 1) {
20+
res++;
21+
}
22+
}
23+
}
24+
free(rows);
25+
free(cols);
26+
return res;
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
impl Solution {
2+
pub fn num_special(mat: Vec<Vec<i32>>) -> i32 {
3+
let m = mat.len();
4+
let n = mat[0].len();
5+
let mut rows = vec![0; m];
6+
let mut cols = vec![0; n];
7+
for i in 0..m {
8+
for j in 0..n {
9+
rows[i] += mat[i][j];
10+
cols[j] += mat[i][j];
11+
}
12+
}
13+
14+
let mut res = 0;
15+
for i in 0..m {
16+
for j in 0..n {
17+
if mat[i][j] == 1 && rows[i] == 1 && cols[j] == 1 {
18+
res += 1;
19+
}
20+
}
21+
}
22+
res
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function numSpecial(mat: number[][]): number {
2+
const m = mat.length;
3+
const n = mat[0].length;
4+
const rows = new Array(m).fill(0);
5+
const cols = new Array(n).fill(0);
6+
for (let i = 0; i < m; i++) {
7+
for (let j = 0; j < n; j++) {
8+
if (mat[i][j] === 1) {
9+
rows[i]++;
10+
cols[j]++;
11+
}
12+
}
13+
}
14+
15+
let res = 0;
16+
for (let i = 0; i < m; i++) {
17+
for (let j = 0; j < n; j++) {
18+
if (mat[i][j] === 1 && rows[i] === 1 && cols[j] === 1) {
19+
res++;
20+
}
21+
}
22+
}
23+
24+
return res;
25+
}

0 commit comments

Comments
 (0)