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 ...
随机推荐
- git 遇到的问题
1.error: RPC failed; curl transfer closed with outstanding read data remaining 解决办法: git clone https ...
- AVL(二叉平衡树) 的实现
一颗AVL树是其每个节点的左子树与右子树的高度最多差1的二叉查找树. 在插入过程中,利用旋转的办法保持这个性质. 共分四种情形: 1. 树T的左孩子的左子树上新插入节点导致破坏平衡性: 如下图左边所 ...
- Linux基础——系统监控
系统监视和进程控制工具——top(任务管理器) top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. 各行数据大致解释如下: 12: ...
- yii根据id查询一条数据
model层 public function selectone($ag_id=''){ return $this->findBySql("SELECT * FROM agency w ...
- http post url参数封装(key token 及校验码)
post请求本来是一种很常见的web请求方式,相信许多项目都有一系列的封装工具类. 今天遇着一个特殊的需求. 需要在post的请求url内封装相应的token 与及key相关的值,这就奇怪了,url封 ...
- nodejs的http.request使用post方式提交数据请求
官方api文档 http://nodejs.org/docs/v0.6.1/api/http.html#http.request虽然也有POST例子,但是并不完整. 直接上代码:http_post.j ...
- kafka--producer配置解析
producer解析 主要是解析一下producer的相关配置以及一些使用场景 相关解析 名称 说明 类型 默认值 有效值 重要性 bootstrap.servers 用于建立与kafka集群连接 ...
- 对 java 设计模式的一些了解 (正在学习整理中)
A .设计模式的作用 从书上摘话给你们看看 帮助我们将应用组织成容易了解,容易维护,具有弹性的架构,建立可维护的OO系统,要诀在于随时想到系统以后可能需要的变化以及应付变化的原则. 这么复杂的解释肯定 ...
- Sybase:获取本月最后一天的日期的实现方法
Sybase:获取本月最后一天的日期的实现方法 Oracle中查询月底那天的日期的函数为:last_day(). 在ASE中没有对应的函数,在Oracle移植到Sybase的时候,需要手动编写函数来实 ...
- adplayer移植【转】
本文转载自:https://blog.csdn.net/qq361294382/article/details/50525412 这两天做madplayer移植,由于是刚装的ubuntu14.04,所 ...