LeetCode OJ:Set Matrix Zeroes(设置矩阵0元素)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
这题要注意的就是应该是就地执行,所以可以先采取一个数组将所有0元素的位置记下来,然后一次性的全部置成0即可:
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int szHor = matrix.size();
if(szHor == ) return;
int szVer = matrix[].size();
if(szVer == ) return;
vector<int> mark;
mark.clear();
int i, j;
for(i = ; i < szHor; ++i){
for(j = ; j < szVer; ++j){
if(matrix[i][j] == ){
mark.push_back(i);
mark.push_back(j);
}
}
}
int row, col;
int sz = mark.size();
for(int p = ; p < sz; p+=){
for(int m = ; m < szHor; m++){
matrix[m][mark[p + ]] = ;
}
for(int n = ; n < szVer; n++){
matrix[mark[p]][n] = ;
}
}
}
};
LeetCode OJ:Set Matrix Zeroes(设置矩阵0元素)的更多相关文章
- leetcode[73] Set Matrix Zeroes 将矩阵置零
给定一个矩阵,把零值所在的行和列都置为零.例如: 1 2 3 1 3 1 1 1 操作之后变为 1 3 0 0 0 1 1 方法1: 赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...
- 【LeetCode每天一题】Set Matrix Zeroes(设置0矩阵)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Exampl ...
- LeetCode第[73]题(Java):Set Matrix Zeroes(矩阵置0)
题目:矩阵置0 难度:Easy 题目内容: Given a m x n matrix, if an element is 0, set its entire row and column to 0. ...
- LeetCode 54. Spiral Matrix(螺旋矩阵)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- leetcode 【 Set Matrix Zeroes 】python 实现
题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cl ...
- [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II
Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...
- [LeetCode] 73. 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. Follow ...
- 【Leetcode】Set Matrix Zeroes
给定一个m x n的矩阵,如果某个元素为0,则把该元素所在行和列全部置0. Given a m x n matrix, if an element is 0, set its entire row a ...
- [LeetCode] 73. 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. Exampl ...
随机推荐
- hadoop namenode
存储文件系统元数据,例如:文件目录结构,不同文件的分块情况,每块存储在那个节点,权限等 这些元数据全部存储在内存中,所以,namenode要求内存比较大 hdfs在存文件的时候会按照块存储,每一块默认 ...
- 基于JSP的学术交流论坛系统的设计与实现
版权声明:本文为[博主](https://zhangkn.github.io)原创文章.未经博主同意不得转载. https://creativecommons.org/licenses/by-nc-s ...
- Win7中的IIS配置asp时出现“出现403 文件夹禁止訪问错误”!
Win7中的IIS配置asp时出现"出现403 文件夹禁止訪问错误"! 在[默认文档]中设一下启动文件即可了.
- vue-cli的utils.js文件详解
转载自:http://www.cnblogs.com/ye-hcj/p/7078047.html utils.js文件 // 引入nodejs路径模块var path = require('path' ...
- case的嵌套使用
case分支语句的格式如下: case $变量名 in 模式1) 命令序列1 ;; 模式2) 命令序列2 ;; *) 默认执行的命令序列 ;; esac case语句结构特点如下: 1. ...
- C# 字符串中正则表达式的应用
1.截取字符串中指定内容 {"weatherinfo":{"city":"北京","cityid":"1010 ...
- c#中的控件01
1.常用控件: 只读文本:TextBlock.文本框:TextBox.按钮:Button 事件:鼠标移到按钮上的时候显示“大爷您来了”,离开 显示“大爷常来”,Click(点击).Loaded(控件加 ...
- React-Navigation与Redux整合详解
本文转自:文章地址:http://blog.csdn.net/u013718120/article/details/72357698 继react-navigation发布已经过去半年的时间,想必Re ...
- Luogu-5004 专心OI-跳房子(矩阵快速幂)
Luogu-5004 专心OI-跳房子(矩阵快速幂) 题目链接 题解: 先考虑最朴素的dp 设\(f[i][0/1]\)表示第\(i\)个位置跳/不跳的方案数,则: \[ \begin{cases} ...
- pbs "ll: command not found"
可以用 ls -ltr 替代 ll