当前位置: 首页 > news >正文

面试经典150题——Day37

文章目录

    • 一、题目
    • 二、题解

一、题目

73. Set Matrix Zeroes

Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0’s.

You must do it in place.

Example 1:

Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]
Example 2:

Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]

Constraints:

m == matrix.length
n == matrix[0].length
1 <= m, n <= 200
-231 <= matrix[i][j] <= 231 - 1

Follow up:

A straightforward 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?

题目来源: leetcode

二、题解

class Solution {
public:void fillZero(vector<vector<int>>& matrix,int row,int col){int m = matrix.size();int n = matrix[0].size();//将行置为0for(int j = 0;j < n;j++){matrix[row][j] = 0;}//将列置为0for(int i = 0;i < m;i++){matrix[i][col] = 0;}}void setZeroes(vector<vector<int>>& matrix) {int m = matrix.size();int n = matrix[0].size();vector<pair<int,int>> rowCols;//存储值为0的元素对应的行和列for(int i = 0;i < m;i++){for(int j = 0;j < n;j++){if(matrix[i][j] == 0) rowCols.push_back({i,j});}}//将对应的行和列填充为0for(int i = 0;i < rowCols.size();i++){fillZero(matrix,rowCols[i].first,rowCols[i].second);}}
};
http://www.lryc.cn/news/225499.html

相关文章:

  • 在 Arduino IDE 2.0 中安装 ESP32 板(Windows、Mac OS X、Linux)
  • 西门子S7-1200PLC混合通信编程(ModbusTcp和UDP通信)
  • Hbase 迁移小结:从实践中总结出的最佳迁移策略
  • 键盘win键无法使用,win+r不生效、win键没反应、Windows键失灵解决方案(亲测可以解决)
  • 1. 深度学习——激活函数
  • chatglm3-6b部署及微调
  • Hive 知识点八股文记录 ——(二)优化
  • 计算机技术专业CSIT883系统分析与项目管理介绍
  • gitlab安装地址
  • Spark处理方法_提取文件名中的时间
  • 技术分享 | 测试平台开发-前端开发之数据展示与分析
  • NZ系列工具NZ06:VBA创建PDF文件说明
  • redis-cli 连接 sentinel架构的redis服务
  • 使用github copilot
  • 1438 绝对差不超过限制的最长连续子数组(单调队列)
  • OpenCV入门9:图像增强和图像滤波
  • Pycharm常用快捷键和替换正则表达式
  • C#,数值计算——函数计算,Epsalg的计算方法与源程序
  • Delphi 12 重返雅典 (RAD Studio 12)
  • 手写链表C++
  • 为什么我一直是机器视觉调机仔,为什么一定要学一门高级语言编程?
  • MongoDB单实例安装(Linux)
  • 各种业务场景调用API代理的API接口教程(附带电商平台api接口商品详情数据接入示例)
  • React-hooks有哪些 包括用法是什么?
  • 根据DataFrame指定的列该列中如果有n个不同元素则将其转化为n行显示explode()
  • 《持续交付:发布可靠软件的系统方法》- 读书笔记(十三)
  • 【Copilot】登录报错 Extension activation failed: “No auth flow succeeded.“(VSCode)
  • uboot - 驱动开发 - dw watchdog
  • 【系统架构设计】架构核心知识: 2.5 软件测试、系统转换计划、系统维护
  • EXPLAIN详解(MySQL)