Jan 20 - Set Matrix Zeros; Array;

Use additional O(m+n) space, simple improvement. Two array, one is of size row, to store the status of each row, whether there is a 0 element in the row. Similarly, the other array of size col, storing the status of each column that whether there is 0 element in the column. Then traverse the arrays, if the current element is true, which represents that there are at least one 0 element in the row(column). Then set all the elements in the row(column) to be zero.

Code:

public class Solution {
    public void setZeroes(int[][] matrix) {
        int row = matrix.length;
        int col = matrix[0].length;
        boolean[] isZeroColumn = new boolean[col];
        boolean[] isZeroRow = new boolean[row];
        for(int i = 0; i < row; i++){
            for(int j = 0; j < col; j++){
                if(matrix[i][j]==0) {
                    isZeroRow[i] = true;
                    isZeroColumn[j] = true;
                }
            }
        }
        for(int i = 0; i < row; i++){
            if(isZeroRow[i]){
                for(int j = 0; j < col; j++) matrix[i][j] = 0;
            }
        }
        for(int i = 0; i < col; i++){
            if(isZeroColumn[i]){
                for(int j = 0; j < row; j++) matrix[j][i] = 0;
            }
        }
    }
}
时间: 2024-08-09 22:00:15

Jan 20 - Set Matrix Zeros; Array;的相关文章

Jan 18 - Spiral Matrix; 2D Array;

代码: public class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> resultList = new ArrayList<>(); int row = matrix.length; if(row == 0) return resultList; int col = matrix[0].length; if(col == 0) return resultL

LeetCode 73. Set Matrix Zeros(矩阵赋零)

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple improvement uses O

numpy中的matrix和array

Preface 在相关聚类算法的实现过程中,用python语言实现,会经常出现array和matrix的混淆,这里做个总结. array数组 numpy中最基本(默认)的类型是array,他的相关操作都是按元素操作的即用作数值计算当中(按元素操作有+,-,,/,*等).相乘举例: from numpy import * >>> a=array([1,2]) >>> a array([1, 2]) >>> b=array([2,3]) >>&

【leetcode】Set Matrix Zeros

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up:Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple improvement uses O(

Python与线性代数——Numpy中的matrix()和array()的区别

Numpy中matrix必须是2维的,但是 numpy中array可以是多维的(1D,2D,3D····ND).matrix是array的一个小的分支,包含于array.所以matrix 拥有array的所有特性. matrix() 和 array() 的区别,主要从以下方面说起: 矩阵生成方式不同 import numpy as np a1 = np.array([[1, 2], [3, 4]]) b1 = np.mat([[1, 2], [3, 4]]) a2 = np.array(([1,

[LintCode] Set Matrix Zeros

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Example Given a matrix [ [1,2], [0,3] ], return[[0,2],[0,0]] Challenge Did you use extra space?A straight forward solution using O(mn) space is probably a b

Set Matrix Zeros

Question: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Solution: 1 class Solution { 2 public: 3 void setZeroes(vector<vector<int>>& matrix) { 4 int m=matrix.size(); 5 int n=matrix[0].size();

[LeetCode] Set Matrix Zeros

1 public class Solution { 2 public void setZeroes(int[][] matrix) { 3 int m=matrix.length; 4 int n=matrix[0].length; 5 boolean hasZeroCol = false, hasZeroRow = false; 6 7 for (int i=0; i<m; i++) 8 if (matrix[i][0] == 0) { hasZeroCol = true; break; }

Jan 23 - Search In Rotated Array II; Array; Binary Search;

public class Solution { public boolean search(int[] nums, int target) { int len = nums.length; if(len == 0) return false; int low = 0; int high = len-1; while(low < high){ int mid = (low+high)/2; // do not concern the integer overrange situation if(n