@Test public void testFillIfZero() { int[][] matrix = new int[][] { { 1, 2 }, { 3, 4 }, { 5, 0 } }; fillIfZero(matrix); assertEquals(0, matrix[0][1]); assertEquals(0, matrix[1][1]); assertEquals(0, matrix[2][0]); assertEquals(0, matrix[2][1]); assertEquals(1, matrix[0][0]); assertEquals(3, matrix[1][0]); }
public static void fillIfZero(int[][] matrix) { int numRows = matrix.length; int numCols = matrix[0].length; int rowWhereZeroFound = -1; int colWhereZeroFound = -1; for (int rowNum = 0; rowNum < numRows; rowNum++) { boolean isZeroFound = false; for (int colNum = 0; colNum < numCols; colNum++) { isZeroFound = (matrix[rowNum][colNum] == 0) | isZeroFound; if (isZeroFound) { LOGGER.debug("Zero found at [{},{}].", rowNum, colNum); colWhereZeroFound = colNum; rowWhereZeroFound = rowNum; break; } } if (isZeroFound) { LOGGER.debug("Setting to zero [{},{}].", rowNum, colWhereZeroFound); matrix[rowNum][colWhereZeroFound] = 0; } } LOGGER.debug("Setting to zero [{}].", rowWhereZeroFound); Arrays.fill(matrix[rowWhereZeroFound], 0); for (int rowNum = 0; rowNum < rowWhereZeroFound; rowNum++) { LOGGER.debug("Setting to zero [{},{}].", rowNum, colWhereZeroFound); matrix[rowNum][colWhereZeroFound] = 0; } }
PracticeQuestionsCh1 { public static void fillIfZero(int[][] matrix) { int numRows = matrix.length; int numCols = matrix[0].length; int rowWhereZeroFound = -1; int colWhereZeroFound = -1; for (int rowNum = 0; rowNum < numRows; rowNum++) { boolean isZeroFound = false; for (int colNum = 0; colNum < numCols; colNum++) { isZeroFound = (matrix[rowNum][colNum] == 0) | isZeroFound; if (isZeroFound) { LOGGER.debug("Zero found at [{},{}].", rowNum, colNum); colWhereZeroFound = colNum; rowWhereZeroFound = rowNum; break; } } if (isZeroFound) { LOGGER.debug("Setting to zero [{},{}].", rowNum, colWhereZeroFound); matrix[rowNum][colWhereZeroFound] = 0; } } LOGGER.debug("Setting to zero [{}].", rowWhereZeroFound); Arrays.fill(matrix[rowWhereZeroFound], 0); for (int rowNum = 0; rowNum < rowWhereZeroFound; rowNum++) { LOGGER.debug("Setting to zero [{},{}].", rowNum, colWhereZeroFound); matrix[rowNum][colWhereZeroFound] = 0; } } }
PracticeQuestionsCh1 { public static void fillIfZero(int[][] matrix) { int numRows = matrix.length; int numCols = matrix[0].length; int rowWhereZeroFound = -1; int colWhereZeroFound = -1; for (int rowNum = 0; rowNum < numRows; rowNum++) { boolean isZeroFound = false; for (int colNum = 0; colNum < numCols; colNum++) { isZeroFound = (matrix[rowNum][colNum] == 0) | isZeroFound; if (isZeroFound) { LOGGER.debug("Zero found at [{},{}].", rowNum, colNum); colWhereZeroFound = colNum; rowWhereZeroFound = rowNum; break; } } if (isZeroFound) { LOGGER.debug("Setting to zero [{},{}].", rowNum, colWhereZeroFound); matrix[rowNum][colWhereZeroFound] = 0; } } LOGGER.debug("Setting to zero [{}].", rowWhereZeroFound); Arrays.fill(matrix[rowWhereZeroFound], 0); for (int rowNum = 0; rowNum < rowWhereZeroFound; rowNum++) { LOGGER.debug("Setting to zero [{},{}].", rowNum, colWhereZeroFound); matrix[rowNum][colWhereZeroFound] = 0; } }  }
PracticeQuestionsCh1 { public static void fillIfZero(int[][] matrix) { int numRows = matrix.length; int numCols = matrix[0].length; int rowWhereZeroFound = -1; int colWhereZeroFound = -1; for (int rowNum = 0; rowNum < numRows; rowNum++) { boolean isZeroFound = false; for (int colNum = 0; colNum < numCols; colNum++) { isZeroFound = (matrix[rowNum][colNum] == 0) | isZeroFound; if (isZeroFound) { LOGGER.debug("Zero found at [{},{}].", rowNum, colNum); colWhereZeroFound = colNum; rowWhereZeroFound = rowNum; break; } } if (isZeroFound) { LOGGER.debug("Setting to zero [{},{}].", rowNum, colWhereZeroFound); matrix[rowNum][colWhereZeroFound] = 0; } } LOGGER.debug("Setting to zero [{}].", rowWhereZeroFound); Arrays.fill(matrix[rowWhereZeroFound], 0); for (int rowNum = 0; rowNum < rowWhereZeroFound; rowNum++) { LOGGER.debug("Setting to zero [{},{}].", rowNum, colWhereZeroFound); matrix[rowNum][colWhereZeroFound] = 0; } }  static boolean isUnique(String s); static boolean isPermutation(String s1, String s2); static String encodeRepeatedChars(final String str); static void fillIfZero(int[][] matrix); static boolean isRotation(String s1, String s2); }
PracticeQuestionsCh1 { public static void fillIfZero(int[][] matrix) { int numRows = matrix.length; int numCols = matrix[0].length; int rowWhereZeroFound = -1; int colWhereZeroFound = -1; for (int rowNum = 0; rowNum < numRows; rowNum++) { boolean isZeroFound = false; for (int colNum = 0; colNum < numCols; colNum++) { isZeroFound = (matrix[rowNum][colNum] == 0) | isZeroFound; if (isZeroFound) { LOGGER.debug("Zero found at [{},{}].", rowNum, colNum); colWhereZeroFound = colNum; rowWhereZeroFound = rowNum; break; } } if (isZeroFound) { LOGGER.debug("Setting to zero [{},{}].", rowNum, colWhereZeroFound); matrix[rowNum][colWhereZeroFound] = 0; } } LOGGER.debug("Setting to zero [{}].", rowWhereZeroFound); Arrays.fill(matrix[rowWhereZeroFound], 0); for (int rowNum = 0; rowNum < rowWhereZeroFound; rowNum++) { LOGGER.debug("Setting to zero [{},{}].", rowNum, colWhereZeroFound); matrix[rowNum][colWhereZeroFound] = 0; } }  static boolean isUnique(String s); static boolean isPermutation(String s1, String s2); static String encodeRepeatedChars(final String str); static void fillIfZero(int[][] matrix); static boolean isRotation(String s1, String s2); static final Logger LOGGER; }