[LC BC] Set Matrix Zeroes 21

YJ·6일 전

LeetCode Bootcamp

목록 보기
5/6

https://www.hackerrank.com/contests/leetcode-bootcamp-week-1/challenges/set-matrix-zeroes-21/problem?isFullScreen=true

문제

2차원 배열 주어지면 해당 요소의 행과 열의 모든 요소 0 으로 (in-place)

답안

public static void setZeroes(List<List<Integer>> grid) {
	int row = grid.size();
	int column = grid.get(0).size();
	int[] rowCheck = new int[row];
	int[] columnCheck = new int[column];
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < column; j++) {
			if (grid.get(i).get(j) == 0) {
				rowCheck[i] = 1;
				columnCheck[j] = 1;
			}
		}
	}
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < column; j++) {
			if (rowCheck[i] == 1 || columnCheck[j] == 1) {
				grid.get(i).remove(j);
				grid.get(i).add(j, 0);
			}
		}
	}    
}

메모

in-place 니까 돌면서 바꾸면 안 되고..
체크해놓고 그 다음에 돌면서 바꿔줘야 된다.
단계적 사고~~

profile
Hello

0개의 댓글