73.设置数组为0
来源:互联网 发布:关联规则挖掘算法程序 编辑:程序博客网 时间:2024/06/10 06:04
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.
测试代码(Python):
class Solution(object): def setZeroes(self, matrix): """ :type matrix: List[List[int]] :rtype: void Do not return anything, modify matrix in-place instead. """ row = [False for r in range(len(matrix))] column = [False for r in range(len(matrix[0]))] for i in range(len(matrix)): for j in range(len(matrix[0])): if matrix[i][j]==0: row[i] = True column[j] = True for i in range(len(matrix)): for j in range(len(matrix[0])): if row[i] or column[j]: matrix[i][j] = 0
性能:
参考答案(c++):
class Solution {public: void setZeroes(vector<vector<int> > &matrix) { int col0 = 1, rows = matrix.size(), cols = matrix[0].size(); for (int i = 0; i < rows; i++) { if (matrix[i][0] == 0) col0 = 0; for (int j = 1; j < cols; j++) if (matrix[i][j] == 0) matrix[i][0] = matrix[0][j] = 0; } for (int i = rows - 1; i >= 0; i--) { for (int j = cols - 1; j >= 1; j--) if (matrix[i][0] == 0 || matrix[0][j] == 0) matrix[i][j] = 0; if (col0 == 0) matrix[i][0] = 0; } }};
性能:
阅读全文
0 0
- 73.设置数组为0
- 如何把数组设置为空数组,即长度为0
- 数组中0所在行列设置为0
- 数组下标为0
- 数组下标为0
- 长度为0数组
- 编写程序,使用指针把一个int型数组的所有元素设置为0
- 如何将数组里的元素设置为容器类型
- 设置radio的值,要设置一个数组,而不能简单的设置为一个字符串
- 数组长度为0和数组为null的区别
- 数组长度为0和数组为null
- java数组长度为0和数组为null
- 【数组】判断数组为空还是长度为0好?
- 长度为0的数组
- 大小为0的数组
- 长度为0的数组
- 柔性数组 元素个数为0的数组 变长数组
- 柔性数组成员 长度为0的数组
- 用于防止协议降级攻击的TLS备用信号加密套件值(SCSV),RFC7507
- ViewFlipper的使用
- 关于 Java 堆栈
- 05:填空:类型转换2
- Spring 入门简单的示例
- 73.设置数组为0
- 【笔记】Python面向对象编程
- Linux内核数据包处理流程-数据包接收(2)
- Bellman-Ford模板
- RL增强学习在无人驾驶中的应用
- 搜狐2018研发工程师秋招笔试题
- 前端程序员分享:我是如何学习JavaScript
- DescriptionResourcePathLocationType Project configuration is not up-to-date with pom.xml. Select
- 枚举实际项目用法