zoukankan      html  css  js  c++  java
  • LeetCode Set Matrix Zeroes

    class Solution {
    public:
        void setZeroes(vector<vector<int> > &matrix) {
            int rows = matrix.size();
            int cols = matrix[0].size();
            
            bool col_has_zero = false;
            bool row_has_zero = false;
            
            for (int i=0; i<cols; i++) {
                if (matrix[0][i] == 0) { 
                    row_has_zero = true;
                    break;
                }
            }
            
            for (int i=0; i<rows; i++) {
                if (matrix[i][0] == 0) {
                    col_has_zero = true;
                    break;
                }
            }
            
            for (int i=1; i<rows; i++) {
                for (int j=1; j<cols; j++) {
                    if (matrix[i][j] != 0) continue;
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
            
            for (int i=1; i<rows; i++) {
                for (int j=1; j<cols; j++) {
                    if (!matrix[i][0] || !matrix[0][j]) {
                        matrix[i][j] = 0;
                    }
                }
            }
    
            for (int i=0; row_has_zero && i<cols; i++) matrix[0][i] = 0;
            for (int i=0; col_has_zero && i<rows; i++) matrix[i][0] = 0;
    
        }
    };

    没意思,搞个几个bit的额外空间会死么

    第二轮:

    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(m + n) space, but still not the best solution.
    Could you devise a constant space solution?

    class Solution {
    public:
        void setZeroes(vector<vector<int> > &matrix) {
            bool first_row_zero = false;
            bool first_col_zero = false;
            
            int rows = matrix.size();
            if (rows < 1) {
                return;
            }
            int cols = matrix[0].size();
            if (cols < 1) {
                return;
            }
            
            for (int i=0; i<cols; i++) {
                if (matrix[0][i] == 0) {
                    first_row_zero = true;
                    break;
                }
            }
            
            for (int i=0; i<rows; i++) {
                if (matrix[i][0] == 0) {
                    first_col_zero = true;
                    break;
                }
            }
            
            for (int i=1; i<rows; i++) {
                for (int j=1; j<cols; j++) {
                    if (matrix[i][j] == 0) {
                        matrix[0][j] = 0;
                        matrix[i][0] = 0;
                    }
                }
            }
            
            for (int i=1; i<rows; i++) {
                if (matrix[i][0] == 0) {
                    for (int j=1; j<cols; j++) matrix[i][j] = 0;
                }
            }
            
            
            for (int i=1; i<cols; i++) {
                if (matrix[0][i] == 0) {
                    for (int j=1; j<rows; j++) matrix[j][i] = 0;
                }
            }
            
            for (int i=0; i<cols && first_row_zero; i++) matrix[0][i] = 0;
            for (int i=0; i<rows && first_col_zero; i++) matrix[i][0] = 0;
        }
    };

    没有一次AC,最后做填充的时候应该出去最左边的列和最上面的行

  • 相关阅读:
    Angular Universal 学习笔记
    SAP Spartacus 如何获得当前渲染页面的 CMS 元数据
    Angular 服务器端渲染的学习笔记(二)
    Angular 服务器端渲染的学习笔记(一)
    第三方外部 Saas提供商如何跟使用 SAP 系统的客户进行对接接口集成
    如何从 SAP Spartacus Product Detail 页面,找到其 Angular 实现 Component 的位置
    具备自动刷新功能的 SAP ABAP ALV 报表
    C++学习目录
    c--条件编译
    c--文件读写--二进制
  • 原文地址:https://www.cnblogs.com/lailailai/p/3853695.html
Copyright © 2011-2022 走看看