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,最后做填充的时候应该出去最左边的列和最上面的行

  • 相关阅读:
    ReactiveCocoa入门教程——第一部分
    How Do I Declare A Block in Objective-C?
    Xcode 6制作动态及静态Framework
    用CocoaPods做iOS程序的依赖管理
    oracle误删除数据恢复
    搭建第一个web项目:quartz+spring实现定时任务
    通过jsoup对网页进行数据抓取。
    使用httpClient模拟登陆开心网过程中登陆成功但是跳转不成功
    service
    搭建第一个web项目:实现用户的增删改查(四)
  • 原文地址:https://www.cnblogs.com/lailailai/p/3853695.html
Copyright © 2011-2022 走看看