码迷,mamicode.com
首页 > 编程语言 > 详细

Java for LeetCode 073 Set Matrix Zeroes

时间:2015-05-17 10:38:44      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

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;
	}

 

Java for LeetCode 073 Set Matrix Zeroes

标签:

原文地址:http://www.cnblogs.com/tonyluis/p/4509206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!