Java for LeetCode 073 Set Matrix Zeroes

时间:2015-05-17 10:38:44   收藏:0   阅读:179

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

解题思路:

用两个boolean数组row col表示行列是否有零即可,JAVA实现如下:

	public void setZeroes(int[][] matrix) {
		if (matrix.length == 0 || matrix[0].length == 0)
			return;
		boolean[] row = new boolean[matrix.length], col = new boolean[matrix[0].length];
		for (int i = 0; i < matrix.length; i++)
			for (int j = 0; j < matrix[0].length; j++)
				if (matrix[i][j] == 0) {
					row[i] = true;
					col[j] = true;
				}
		for (int i = 0; i < matrix.length; i++)
			for (int j = 0; j < matrix[i].length; j++)
				if (row[i] || col[j])
					matrix[i][j] = 0;
	}

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!