首页 > 其他 > 详细

清除行列

时间:2017-04-01 23:04:39      阅读:284      评论:0      收藏:0      [点我收藏+]

题目描述

请编写一个算法,若N阶方阵中某个元素为0,则将其所在的行与列清零。

给定一个N阶方阵int[][](C++中为vector>)mat和矩阵的阶数n,请返回完成操作后的int[][]方阵(C++中为vector>),保证n小于等于300,矩阵中的元素为int范围内。

测试样例:
[[1,2,3],[0,1,2],[0,0,1]]
返回:[[0,0,3],[0,0,0],[0,0,0]]
class Clearer {
public:
    vector<vector<int>> clearZero(vector<vector<int>> mat, int n) {
        // write code here
        //vector<vector<int>> res;
        if(mat.size() == 0)
            return mat;
        
        int rowSize = mat.size();
        int colSize = mat[0].size();
        
        bool row[300] = {0};
        bool col[300] = {0};
        
        for(int i = 0;i < rowSize; i++){
            for(int j = 0;j < colSize; j++){
                if(mat[i][j] == 0){
                    row[i] = true;
                    col[j] = true;
                }
            }
        }
          
        for(int i = 0;i < rowSize;i++){
            for(int j = 0;j < colSize;j++){
                if(row[i] || col[j]){
                    mat[i][j] = 0;
                }
            }
        }
        
        return mat;
    }
};

 

清除行列

原文:http://www.cnblogs.com/xiuxiu55/p/6657666.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!