Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfa4cfdd

Browse files
Add c++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent0b9d5d8 commitfa4cfdd

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

‎0073_set_matrix_zeroes/set_zero.c‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
#include<stdio.h>
22
#include<stdlib.h>
33

4-
staticvoidsetZeroes(int**matrix,intmatrixRowSize,intmatrixColSize)
4+
5+
voidsetZeroes(int**matrix,intmatrixSize,int*matrixColSize)
56
{
6-
introw,col,bRow=0,bCol=0;
7-
for (row=0;row<matrixRowSize;row++) {
8-
for (col=0;col<matrixColSize;col++) {
7+
introw,col;
8+
boolbRow= false,bCol= false;
9+
for (row=0;row<matrixSize;row++) {
10+
for (col=0;col<matrixColSize[row];col++) {
911
if (matrix[row][col]==0) {
10-
if (row==0)bCol=1;
11-
if (col==0)bRow=1;
12+
if (row==0)bRow=true;
13+
if (col==0)bCol=true;
1214
matrix[0][col]=matrix[row][0]=0;
1315
}
1416
}
1517
}
1618

17-
for (row=1;row<matrixRowSize;row++) {
18-
for(col=1;col<matrixColSize;col++){
19+
for (row=1;row<matrixSize;row++) {
20+
for(col=1;col<matrixColSize[row];col++){
1921
if (matrix[0][col]==0||matrix[row][0]==0) {
2022
matrix[row][col]=0;
2123
}
2224
}
2325
}
2426

2527
if (bRow) {
26-
for(row=0;row<matrixRowSize;row++) {
27-
matrix[row][0]=0;
28-
}
28+
memset(matrix[0],0,matrixColSize[0]*sizeof(int));
2929
}
3030

3131
if (bCol) {
32-
for (col=0;col<matrixColSize;col++) {
33-
matrix[0][col]=0;
32+
for (row=0;row<matrixSize;row++) {
33+
matrix[row][0]=0;
3434
}
3535
}
3636
}

‎0073_set_matrix_zeroes/set_zero.cc‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<bits/stdc++.h>
2+
3+
usingnamespacestd;
4+
5+
public:
6+
voidsetZeroes(vector<vector<int>>& matrix) {
7+
bool bRow =false, bCol =false;
8+
for (int row =0; row < matrix.size(); row++) {
9+
for (int col =0; col < matrix[row].size(); col++) {
10+
if (matrix[row][col] ==0) {
11+
if (row ==0) { bRow =true; }
12+
if (col ==0) { bCol =true; }
13+
matrix[0][col] = matrix[row][0] =0;
14+
}
15+
}
16+
}
17+
18+
for (int row =1; row < matrix.size(); row++) {
19+
for (int col =1; col < matrix[row].size(); col++) {
20+
if (matrix[0][col] ==0 || matrix[row][0] ==0) {
21+
matrix[row][col] =0;
22+
}
23+
}
24+
}
25+
26+
if (bRow) {
27+
for (auto& m : matrix[0]) {
28+
m =0;
29+
}
30+
}
31+
32+
if (bCol) {
33+
for (int row =0; row < matrix.size(); row++) {
34+
matrix[row][0] =0;
35+
}
36+
}
37+
}
38+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp