Update SetMatrixToZero.java
This commit is contained in:
parent
dbd9448e99
commit
306d2da98c
@ -61,64 +61,87 @@ public class SetMatrixToZero{
|
|||||||
matrix[i][col] = 0;
|
matrix[i][col] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setZeroesByOptimalApproach(int[][] matrix){
|
|
||||||
int rows = matrix.length;
|
|
||||||
int cols = matrix[0].length;
|
|
||||||
|
|
||||||
boolean firstRowZero = false;
|
static boolean hasZeroInFirstRow(int[][] matrix, int cols) {
|
||||||
boolean firstColZero = false;
|
for (int j = 0; j < cols; j++) {
|
||||||
|
|
||||||
for(int j = 0; j < cols; j++){
|
|
||||||
if (matrix[0][j] == 0) {
|
if (matrix[0][j] == 0) {
|
||||||
firstRowZero = true;
|
return true;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
for(int i = 0; i < rows; i++){
|
static boolean hasZeroInFirstCol(int[][] matrix, int rows) {
|
||||||
|
for (int i = 0; i < rows; i++) {
|
||||||
if (matrix[i][0] == 0) {
|
if (matrix[i][0] == 0) {
|
||||||
firstColZero = true;
|
return true;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
for(int i = 1; i < rows; i++){
|
}
|
||||||
for(int j = 1; j < cols; j++){
|
|
||||||
if (matrix[i][j] == 0){
|
static void markZeroesInMatrix(int[][] matrix, int rows, int cols) {
|
||||||
|
for (int i = 1; i < rows; i++) {
|
||||||
|
for (int j = 1; j < cols; j++) {
|
||||||
|
if (matrix[i][j] == 0) {
|
||||||
matrix[i][0] = 0;
|
matrix[i][0] = 0;
|
||||||
matrix[0][j] = 0;
|
matrix[0][j] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
for(int i = 1; i < rows; i++){
|
|
||||||
if(matrix[i][0] == 0){
|
static void setZeroesInRows(int[][] matrix, int rows, int cols) {
|
||||||
for(int j = 1; j < cols; j++){
|
for (int i = 1; i < rows; i++) {
|
||||||
|
if (matrix[i][0] == 0) {
|
||||||
|
for (int j = 1; j < cols; j++) {
|
||||||
matrix[i][j] = 0;
|
matrix[i][j] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int j = 1; j < cols; j++){
|
|
||||||
if(matrix[0][j] == 0) {
|
|
||||||
for(int i = 1; i < rows; i++){
|
|
||||||
matrix[i][j] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(firstRowZero){
|
|
||||||
for(int j = 0; j < cols; j++){
|
|
||||||
matrix[0][j] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(firstColZero){
|
|
||||||
for(int i = 0; i < rows; i++){
|
|
||||||
matrix[i][0] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void setZeroesInCols(int[][] matrix, int rows, int cols) {
|
||||||
|
for (int j = 1; j < cols; j++) {
|
||||||
|
if (matrix[0][j] == 0) {
|
||||||
|
for (int i = 1; i < rows; i++) {
|
||||||
|
matrix[i][j] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void setZeroesInFirstRow(int[][] matrix, int cols) {
|
||||||
|
for (int j = 0; j < cols; j++) {
|
||||||
|
matrix[0][j] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void setZeroesInFirstCol(int[][] matrix, int rows) {
|
||||||
|
for (int i = 0; i < rows; i++) {
|
||||||
|
matrix[i][0] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void setZeroesByOptimalApproach(int[][] matrix) {
|
||||||
|
int rows = matrix.length;
|
||||||
|
int cols = matrix[0].length;
|
||||||
|
|
||||||
|
boolean firstRowZero = hasZeroInFirstRow(matrix, cols);
|
||||||
|
boolean firstColZero = hasZeroInFirstCol(matrix, rows);
|
||||||
|
|
||||||
|
markZeroesInMatrix(matrix, rows, cols);
|
||||||
|
|
||||||
|
setZeroesInRows(matrix, rows, cols);
|
||||||
|
setZeroesInCols(matrix, rows, cols);
|
||||||
|
|
||||||
|
if (firstRowZero) {
|
||||||
|
setZeroesInFirstRow(matrix, cols);
|
||||||
|
}
|
||||||
|
if (firstColZero) {
|
||||||
|
setZeroesInFirstCol(matrix, rows);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user