当前位置:网站首页>Leetcode73. Matrix Zeroing
Leetcode73. Matrix Zeroing
2022-08-01 17:59:00 【Java Full Stack R&D Alliance】
题目传送地址: https://leetcode.cn/problems/set-matrix-zeroes/
运行效率
代码如下:
class Solution {
public static void setZeroes(int[][] matrix) {
//First find a number that does not exist in the matrix, At that time, set the horizontal and vertical positions to this number,Finally, put in the matrix0After it's all done,Change the number to 0
BitSet bitSet = new BitSet();
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] <= 400 && matrix[i][j] >= 0) {
bitSet.set(matrix[i][j]);
}
}
}
int val = bitSet.nextClearBit(0);
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[0].length; col++) {
if (matrix[row][col] == 0) {
fillVal(row, col, matrix, val);
}
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == val) {
matrix[i][j] = 0;
}
}
}
}
public static void fillVal(int row, int col, int[][] matrix, int val) {
//Set everything on the horizontal line of the coordinate to 0, 除非遇到matrix里自带的0才截止
for (int i =0; i < matrix[0].length; i++) {
if (matrix[row][i] != 0) {
matrix[row][i] = val;
}
}
//Set everything on the vertical line of the coordinate to 0, 除非遇到matrix里自带的0才截止
for (int j =0; j < matrix.length; j++) {
if (matrix[j][col] != 0) {
matrix[j][col] = val;
}
}
}
}
边栏推荐
猜你喜欢
随机推荐
EpiSci|片上系统的深度强化学习:神话与现实
公用函数----mfc
B011 - 基于51的多功能指纹智能锁
统信软件、龙芯中科等四家企业共同发布《数字办公安全创新方案》
棕榈油罐区数字化转型
QPalette调色板、框架色彩填充
加州大学|通过图抽象从不同的第三人称视频中进行逆强化学习
开发工具:第五章:使用idea生成实体类
QT基础功能,信号、槽
LeaRun.net快速开发动态表单
C语言理论--笔试面试基础稳固
极化微波成像概述
Shell nl命令详解(显示行号、读取文件)
2022年SQL经典面试题总结(带解析)
程序员架构修炼之道:如何设计“易理解”的系统架构?
数字化采购管理系统开发:精细化采购业务流程管理,赋能企业实现“阳光采购”
广汽埃安“弹匣电池”,四大核心技术,出行安全保障
TCP million concurrent server optimization parameters
想做期货,农产品期货怎么炒?波动大么
matlab 基于奇偶校验的LSB隐藏水印 三种改进









