问题:给定大小为MXN的布尔矩阵mat[M][N],请对其进行修改,以使如果矩阵单元mat[i][j]为1(或true),则使第i行和第j列的所有单元格均为1。
Example1ThematrixshouldbechangedtofollowingExample2ThematrixshouldbechangedtofollowingExample3Thematrix10010000shouldbechangedtofollowing11111111复制ErrorOK!
方法1(使用两个临时数组):
创建两个临时数组row[M]和col[N]。将row[]和col[]的所有值初始化为0。遍历输入矩阵mat[M][N]。如果看到输入mat[i][j]为true,则将row[i]和col[j]标记为true。再次遍历输入矩阵mat[M][N]。对于每个入口mat[i][j],检查row[i]和col[j]的值。如果两个值(row[i]或col[j])中的任何一个为true,则将mat[i][j]标记为true。
感谢DixitSethi提出了这种方法。
C++
//C++CodeForABooleanMatrixQuestion#includebits/stdc++.husingnamespacestd;#defineR3#defineC4voidmodifyMatrix(boolmat[R][C]){boolrow[R];boolcol[C];inti,j;/*Initializeallvaluesofrow[]as0*/for(i=0;iR;i++){row[i]=0;}/*Initializeallvaluesofcol[]as0*/for(i=0;iC;i++){col[i]=0;}//Stotherowsandcolumnstobemarkedas//1inrow[]andcol[]arraysspectivelyfor(i=0;iR;i++){for(j=0;jC;j++){if(mat[i][j]==1){row[i]=1;col[j]=1;}}}//Modifytheinputmatrixmat[]usingthe//aboveconstructedrow[]andcol[]arraysfor(i=0;iR;i++){for(j=0;jC;j++){if(row[i]==1
col[j]==1){mat[i][j]=1;}}}}/*Autilityfunctiontoprinta2Dmatrix*/voidprintMatrix(boolmat[R][C]){inti,j;for(i=0;iR;i++){for(j=0;jC;j++){coutmat[i][j];}coutendl;}}//DriverCodeintmain(){boolmat[R][C]={{1,0,0,1},{0,0,1,0},{0,0,0,0}};cout"InputMatrix\n";printMatrix(mat);modifyMatrix(mat);printf("Matrixaftermodification\n");printMatrix(mat);turn0;}//Thiscodeiscontributed//byAkankshaRai(Abby_akku)复制ErrorOK!
Java
//JavaCodeForABooleanMatrixQuestionclassGFG{publicstaticvoidmodifyMatrix(intmat[][],intR,intC){introw[]=newint[R];intcol[]=newint[C];inti,j;/*Initializeallvaluesofrow[]as0*/for(i=0;iR;i++){row[i]=0;}/*Initializeallvaluesofcol[]as0*/for(i=0;iC;i++){col[i]=0;}/*Stotherowsandcolumnstobemarkedas1inrow[]andcol[]arraysspectively*/for(i=0;iR;i++){for(j=0;jC;j++){if(mat[i][j]==1){row[i]=1;col[j]=1;}}}/*Modifytheinputmatrixmat[]usingtheaboveconstructedrow[]andcol[]arrays*/for(i=0;iR;i++){for(j=0;jC;j++){if(row[i]==1
col[j]==1){mat[i][j]=1;}}}}/*Autilityfunctiontoprinta2Dmatrix*/publicstaticvoidprintMatrix(intmat[][],intR,intC){inti,j;for(i=0;iR;i++){for(j=0;jC;j++){System.out.print(mat[i][j]+"");}System.out.println();}}/*Driverprogramtotestabovefunctions*/publicstaticvoidmain(String[]args){intmat[][]={{1,0,0,1},{0,0,1,0},{0,0,0,0},};System.out.println("MatrixIntially");printMatrix(mat,3,4);modifyMatrix(mat,3,4);System.out.println("Matrixaftermodificationn");printMatrix(mat,3,4);}}//ThiscodeiscontributedbyKamalRawal复制ErrorOK!
Python3
#Python3CodeForABooleanMatrixQuestionR=3C=4defmodifyMatrix(mat):row=[0]*Rcol=[0]*C#Initializeallvaluesofrow[]as0foriinrange(0,R):row[i]=0#Initializeallvaluesofcol[]as0foriinrange(0,C):col[i]=0#Stotherowsandcolumnstobemarked#as1inrow[]andcol[]arraysspectivelyforiinrange(0,R):forjinrange(0,C):if(mat[i][j]==1):row[i]=1col[j]=1#Modifytheinputmatrixmat[]usingthe#aboveconstructedrow[]andcol[]arraysforiinrange(0,R):forjinrange(0,C):if(row[i]==1orcol[j]==1):mat[i][j]=1#Autilityfunctiontoprinta2DmatrixdefprintMatrix(mat):foriinrange(0,R):forjinrange(0,C):print(mat[i][j],end="")print()#DriverCodemat=[[1,0,0,1],[0,0,1,0],[0,0,0,0]]print("InputMatrixn")printMatrix(mat)modifyMatrix(mat)print("Matrixaftermodificationn")printMatrix(mat)#ThiscodeiscontributedbyNikitaTiwari.复制ErrorOK!
C#
//C#CodeForABoolean//MatrixQuestionusingSystem;classGFG{publicstaticvoidmodifyMatrix(int[,]mat,intR,intC){int[]row=newint[R];int[]col=newint[C];inti,j;/*Initializeallvaluesofrow[]as0*/for(i=0;iR;i++){row[i]=0;}/*Initializeallvaluesofcol[]as0*/for(i=0;iC;i++){col[i]=0;}/*Stotherowsandcolumnstobemarkedas1inrow[]andcol[]arraysspectively*/for(i=0;iR;i++){for(j=0;jC;j++){if(mat[i,j]==1){row[i]=1;col[j]=1;}}}/*Modifytheinputmatrixmat[]usingtheaboveconstructedrow[]andcol[]arrays*/for(i=0;iR;i++){for(j=0;jC;j++){if(row[i]==1
col[j]==1){mat[i,j]=1;}}}}/*Autilityfunctiontoprinta2Dmatrix*/publicstaticvoidprintMatrix(int[,]mat,intR,intC){inti,j;for(i=0;iR;i++){for(j=0;jC;j++){Console.Write(mat[i,j]+"");}Console.WriteLine();}}//DrivercodestaticpublicvoidMain(){int[,]mat={{1,0,0,1},{0,0,1,0},{0,0,0,0}};Console.WriteLine("MatrixIntially");printMatrix(mat,3,4);modifyMatrix(mat,3,4);Console.WriteLine("Matrixafter"+"modificationn");printMatrix(mat,3,4);}}//Thiscodeiscontributedbyajit复制ErrorOK!
PHP
?php//PHPCodeForABoolean//MatrixQuestionR=3;C=4;functionmodifyMatrix(mat){globalR,C;row=array();col=array();/*Initializeallvaluesofrow[]as0*/for(i=0;iR;i++){row[i]=0;}/*Initializeallvaluesofcol[]as0*/for(i=0;iC;i++){col[i]=0;}/*Stotherowsandcolumnstobemarkedas1inrow[]andcol[]arraysspectively*/for(i=0;iR;i++){for(j=0;jC;j++){if(mat[i][j]==1){row[i]=1;col[j]=1;}}}/*Modifytheinputmatrixmat[]usingtheaboveconstructedrow[]andcol[]arrays*/for(i=0;iR;i++){for(j=0;jC;j++){if(row[i]==1
col[j]==1){mat[i][j]=1;}}}}/*Autilityfunctiontoprinta2Dmatrix*/functionprintMatrix(mat){globalR,C;for(i=0;iR;i++){for(j=0;jC;j++){echomat[i][j]."";}echo"\n";}}//Drivercodemat=array(array(1,0,0,1),array(0,0,1,0),array(0,0,0,0));echo"InputMatrix\n";printMatrix(mat);modifyMatrix(mat);echo"Matrixaftermodification\n";printMatrix(mat);//Thiscodeiscontributed//byChitraNayal?复制ErrorOK!
输出:
InputMatrix10010000Matrixaftermodification11111111复制ErrorOK!
时间复杂度:O(M*N)。
辅助空间:O(M+N)。
方法2(方法1的空间优化版本):
该方法是上述方法1的空间优化版本。该方法使用输入矩阵的第一行和第一列代替方法1的辅助数组row[]和col[]。因此,我们要做的是:首先注意第一行和第一列,并将这两个信息存储在两个标志变量rowFlag和colFlag中。获得此信息后,我们可以将第一行和第一列用作辅助数组,并将方法1应用于大小为(M-1)*(N-1))的子矩阵(不包括第一行和第一列的矩阵)。
1)扫描第一行并设置一个变量rowFlag以指示是否需要在第一行中设置全1。2)扫描第一列并设置变量colFlag以指示是否需要在第一列中设置全1。3)分别使用第一行和第一列作为辅助数组row[]和col[],将矩阵视为从第二行和第二列开始的子矩阵,并应用方法1。4)最后,如果需要,使用rowFlag和colFlag更新第一行和第一列。
时间复杂度:O(M*N)。
辅助空间:O(1)。
感谢Sidh提出了这种方法。
C++
#includebits/stdc++.husingnamespacestd;#defineR3#defineC4voidmodifyMatrix(intmat[R][C]){//variablestocheckiftheaany1//infirstrowandcolumnboolrow_flag=false;boolcol_flag=false;//updatingthefirstrowandcolif1//isencountedfor(inti=0;iR;i++){for(intj=0;jC;j++){if(i==0mat[i][j]==1)row_flag=true;if(j==0mat[i][j]==1)col_flag=true;if(mat[i][j]==1){mat[0][j]=1;mat[i][0]=1;}}}//Modifytheinputmatrixmat[]usingthe//firstrowandfirstcolumnofMatrixmatfor(inti=1;iR;i++){for(intj=1;jC;j++){if(mat[0][j]==1
mat[i][0]==1){mat[i][j]=1;}}}//modifyfirstrowifthewasany1if(row_flag==true){for(inti=0;iC;i++){mat[0][i]=1;}}//modifyfirstcolifthewasany1if(col_flag==true){for(inti=0;iR;i++){mat[i][0]=1;}}}/*Autilityfunctiontoprinta2Dmatrix*/voidprintMatrix(intmat[R][C]){for(inti=0;iR;i++){for(intj=0;jC;j++){coutmat[i][j];}cout"\n";}}//Driverfunctiontotesttheabovefunctionintmain(){intmat[R][C]={{1,0,0,1},{0,0,1,0},{0,0,0,0}};cout"InputMatrix:\n";printMatrix(mat);modifyMatrix(mat);cout"MatrixAfterModification:\n";printMatrix(mat);turn0;}//ThiscodeiscontributedbyNikitaTiwari复制ErrorOK!
Java
classGFG{publicstaticvoidmodifyMatrix(intmat[][]){//variablestocheckiftheaany1//infirstrowandcolumnbooleanrow_flag=false;booleancol_flag=false;//updatingthefirstrowandcolif1//isencountedfor(inti=0;imat.length;i++){for(intj=0;jmat[0].length;j++){if(i==0mat[i][j]==1)row_flag=true;if(j==0mat[i][j]==1)col_flag=true;if(mat[i][j]==1){mat[0][j]=1;mat[i][0]=1;}}}//Modifytheinputmatrixmat[]usingthe//firstrowandfirstcolumnofMatrixmatfor(inti=1;imat.length;i++){for(intj=1;jmat[0].length;j++){if(mat[0][j]==1
mat[i][0]==1){mat[i][j]=1;}}}//modifyfirstrowifthewasany1if(row_flag==true){for(inti=0;imat[0].length;i++){mat[0][i]=1;}}//modifyfirstcolifthewasany1if(col_flag==true){for(inti=0;imat.length;i++){mat[i][0]=1;}}}/*Autilityfunctiontoprinta2Dmatrix*/publicstaticvoidprintMatrix(intmat[][]){for(inti=0;imat.length;i++){for(intj=0;jmat[0].length;j++){System.out.print(mat[i][j]);}System.out.println("");}}//Driverfunctiontotesttheabovefunctionpublicstaticvoidmain(Stringargs[]){intmat[][]={{1,0,0,1},{0,0,1,0},{0,0,0,0}};System.out.println("InputMatrix:");printMatrix(mat);modifyMatrix(mat);System.out.println("MatrixAfterModification:");printMatrix(mat);}}//ThiscodeiscontributedbyArnavKr.Mandal.复制ErrorOK!
Python3
#Python3CodeForABooleanMatrixQuestiondefmodifyMatrix(mat):#variablestocheckiftheaany1#infirstrowandcolumnrow_flag=Falsecol_flag=False#updatingthefirstrowandcol#if1isencountedforiinrange(0,len(mat)):forjinrange(0,len(mat)):if(i==0andmat[i][j]==1):row_flag=Trueif(j==0andmat[i][j]==1):col_flag=Trueif(mat[i][j]==1):mat[0][j]=1mat[i][0]=1#Modifytheinputmatrixmat[]usingthe#firstrowandfirstcolumnofMatrixmatforiinrange(1,len(mat)):forjinrange(1,len(mat)+1):if(mat[0][j]==1ormat[i][0]==1):mat[i][j]=1#modifyfirstrowifthewasany1if(row_flag==True):foriinrange(0,len(mat)):mat[0][i]=1#modifyfirstcolifthewasany1if(col_flag==True):foriinrange(0,len(mat)):mat[i][0]=1#Autilityfunctiontoprinta2DmatrixdefprintMatrix(mat):foriinrange(0,len(mat)):forjinrange(0,len(mat)+1):print(mat[i][j],end="")print()#DriverCodemat=[[1,0,0,1],[0,0,1,0],[0,0,0,0]]print("InputMatrix:")printMatrix(mat)modifyMatrix(mat)print("MatrixAfterModification:")printMatrix(mat)#ThiscodeiscontributedbyNikitatiwari.复制ErrorOK!
C#
//C#CodeForABoolean//MatrixQuestionusingSystem;classGFG{publicstaticvoidmodifyMatrix(int[,]mat){//variablestocheck//iftheaany1//infirstrowandcolumnboolrow_flag=false;boolcol_flag=false;//updatingthefirst//rowandcolif1//isencountedfor(inti=0;imat.GetLength(0);i++){for(intj=0;jmat.GetLength(1);j++){if(i==0mat[i,j]==1)row_flag=true;if(j==0mat[i,j]==1)col_flag=true;if(mat[i,j]==1){mat[0,j]=1;mat[i,0]=1;}}}//Modifytheinputmatrixmat[]//usingthefirstrowandfirst//columnofMatrixmatfor(inti=1;imat.GetLength(0);i++){for(intj=1;jmat.GetLength(1);j++){if(mat[0,j]==1
mat[i,0]==1){mat[i,j]=1;}}}//modifyfirstrow//ifthewasany1if(row_flag==true){for(inti=0;imat.GetLength(1);i++){mat[0,i]=1;}}//modifyfirstcolif//thewasany1if(col_flag==true){for(inti=0;imat.GetLength(0);i++){mat[i,0]=1;}}}/*Autilityfunctiontoprinta2Dmatrix*/publicstaticvoidprintMatrix(int[,]mat){for(inti=0;imat.GetLength(0);i++){for(intj=0;jmat.GetLength(1);j++){Console.Write(mat[i,j]+"");}Console.Write("\n");}}//DriverCodepublicstaticvoidMain(){int[,]mat={{1,0,0,1},{0,0,1,0},{0,0,0,0}};Console.Write("InputMatrix:\n");printMatrix(mat);modifyMatrix(mat);Console.Write("MatrixAfter"+"Modification:\n");printMatrix(mat);}}//Thiscodeiscontributed//byChitraNayal复制ErrorOK!
PHP
?php//PHPCodeForABoolean//MatrixQuestionR=3;C=4;functionmodifyMatrix(mat){globalR,C;//variablestocheckif//theaany1in//firstrowandcolumnrow_flag=false;col_flag=false;//updatingthefirst//rowandcolif1//isencountedfor(i=0;iR;i++){for(j=0;jC;j++){if(i==0mat[i][j]==1)row_flag=true;if(j==0mat[i][j]==1)col_flag=true;if(mat[i][j]==1){mat[0][j]=1;mat[i][0]=1;}}}//Modifytheinputmatrix//mat[]usingthefirst//rowandfirstcolumnof//Matrixmatfor(i=1;iR;i++){for(j=1;jC;j++){if(mat[0][j]==1
mat[i][0]==1){mat[i][j]=1;}}}//modifyfirstrow//ifthewasany1if(row_flag==true){for(i=0;iC;i++){mat[0][i]=1;}}//modifyfirstcol//ifthewasany1if(col_flag==true){for(i=0;iR;i++){mat[i][0]=1;}}}/*Autilityfunctiontoprinta2Dmatrix*/functionprintMatrix(mat){globalR,C;for(i=0;iR;i++){for(j=0;jC;j++){echomat[i][j]."";}echo"\n";}}//DriverCodemat=array(array(1,0,0,1),array(0,0,1,0),array(0,0,0,0));echo"InputMatrix:\n";printMatrix(mat);modifyMatrix(mat);echo"MatrixAfterModification:\n";printMatrix(mat);//Thiscodeisconrtributed//byChitraNayal?复制ErrorOK!
输出:
InputMatrix:10010000MatrixAfterModification:11111111