`
hcx2013
  • 浏览: 88965 次
社区版块
存档分类
最新评论

Set Matrix Zeroes

 
阅读更多

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

 

public class Solution {
    public void setZeroes(int[][] matrix) {
        boolean[] rows = new boolean[matrix.length];
        boolean[] cols = 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) {
        			rows[i] = true;
        			cols[j] = true;
        		}
        	}
        }
        for (int i = 0; i < matrix.length; i++) {
        	for (int j = 0; j < matrix[0].length; j++) {
        		if (rows[i] || cols[j]) {
        			matrix[i][j] = 0;
        		}
        	}
        }
    }
}

 

分享到:
评论

相关推荐

    javalruleetcode-SDE-Problems:标准SDE问题列表

    Zeros Pascal Triangle Next Permutation Inversion of Array (Using Merge Sort) Stock Buy and Sell Rotate Matrix 第3天:(数学) Excel 列号 在 log N 中查找 n^x 在数字的阶乘中计算尾随零 在 Log N 网格中...

    set_up_a_new_matrix.rar_4 3 2 1_生成矩阵_矩阵

    本教程“set_up_a_new_matrix.rar_4 3 2 1_生成矩阵_矩阵”深入探讨了如何在MATLAB中构建和管理各种类型的矩阵。以下是关于这个主题的详细讲解: 1. **建立新矩阵**: - 基础矩阵创建:MATLAB允许直接通过等号(=...

    ndarray-matrix-vector-multiply:密集矩阵向量乘法

    例子 var mvp = require ( "ndarray-matrix-vector-product" )var zeros = require ( "zeros" )//Initialize some vectors and a matrixvar x = zeros ( [ 16 ] )var M = zeros ( [ 16 , 8 ] )var y = zeros ( [ 8 ]...

    数学建模MATLAB代码最小生成树MATLAB程序直接调用版

    selectedNode = zeros(1,n); % selectedNode[i] == 1 means node i is included in the tree noEdge = 0; % number of edges in the tree selectedNode(1) = 1; T = []; while noEdge minimum = Inf; x = 0; y =...

    DMC算法,经典

    DMC(Dynamic Matrix Control)是一种模型预测控制算法,广泛应用于过程控制领域。下面将详细介绍DMC算法的原理、实现和应用。 DMC算法原理 DMC算法的核心是使用动态矩阵来描述系统的输入输出关系。动态矩阵是系统...

    i-vector的工具箱

    This function implements global cepstral mean and variance normalization (CMVN) on input feature matrix fea to remove the linear channel effects. The code assumes that there is one observation per ...

    Eigen_Matlab指令对照.pdf

    此外,Eigen还提供了setIdentity(), setZero(), setOnes()等方法来快速设置矩阵为单位矩阵、零矩阵和全1矩阵。这些方法都有对应的Matlab函数,比如eye(), zeros()和ones()。 Eigen和Matlab在随机数生成和矩阵切片...

    IDL Analyst.pdf

    - **IMSL_STEPWISE**: Stepwise regression is a method for selecting a subset of predictor variables from a larger set based on statistical criteria. `IMSL_STEPWISE` automates this process, making it ...

    Matters Computational-ideas, algorithms, source code

    - **Extraction of Ones, Zeros, or Blocks Near Transitions**: Strategies for extracting specific patterns from a word. - **Computing the Index of a Single Set Bit**: Efficient algorithms for finding ...

    Matters Computational

    - **Extraction of Ones, Zeros, or Blocks Near Transitions**: Algorithms for extracting specific patterns from binary data. - **Computing the Index of a Single Set Bit**: Efficient ways to find the ...

    使用pyplot.matshow()函数添加绘图标题

    aa = np.zeros(dims) for i in range(min(dims)): aa[i, i] = i return aa # 创建一个15x15的矩阵 matrix = samplemat((15, 15)) # 创建一个figure对象 fig = plt.figure() # 添加一个子图,并设置为整个...

    MATLAB程序设计

    error('Matrix A must be 256x256.'); end % 检查n的有效性 if n | n &gt; M*N error('n must be within the range [1, 256*256].'); end % 获取矩阵A的所有元素的绝对值 absA = abs(A); % 将绝对值矩阵...

    基于lf蚁群聚类算法

    tabu=zeros(m.n)l for k=1:m tabu(k,g(NC,k))=1; maxp(1)=0; for j=1:n if tabu(k,j)==0 psum_medium0(1,j)=(tao(g(NC,k)^alpha).*(yita(g(NC,k),j)^beta); else psum_medium0(1,j)=0; end; psum_medium=psum_medium...

    曲线拟合工具CurveExpert 1.0

    added points onto the end of the data set. Fixed. + If a graph window was closed and then reopened (except after a CurveFinder operation), the comments in the Graph Info dialog were not correct. ...

    occam一维反演

    C OBJMAT(IRUF, DEL, NPEN), ASSEMBLES THE PENALTY MATRIX (THIS WILL BE DEPENDENT ON MODEL C TYPE AND DIMENSION). OBJMAT IS ALSO RESPONSIBLE FOR SETTING ANY C WEIGHTS, /RESULT/PREWTS(), ASSOCIATED WITH...

    python3.6环境下如何安装freetype库和基本使用方法

    self._face.set_transform(matrix, pen_translate) self._face.load_char(cur_char) # ...(省略部分代码) ``` `draw_string`方法内部使用了FreeType的API来处理文本渲染,包括设置字符变形矩阵、加载字符并计算...

    Algorithm for programmer

    - **1.4 Extraction of ones, zeros, or blocks near transitions**: 如何提取出接近转换点的1、0或者块。 - **1.5 Computing the index of a single set bit**: 如何计算单个设置位的索引位置。 - **1.6 ...

Global site tag (gtag.js) - Google Analytics