当前位置:网站首页>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;
}
}
}
}
边栏推荐
猜你喜欢
随机推荐
【TDP加码福利】COS用户实践征文月,等你来投稿!!!
【Day_11 0506】 最近公共祖先
缓存一致性MESI与内存屏障
tooltip 控件
史上最全的Redis基础+进阶项目实战总结笔记
opencv real-time face detection
【Day_08 0426】两种排序方法
SQL函数 TO_CHAR(一)
OnePlus 10RT appears on Geekbench, product launch also seems to be approaching
【Error】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat’)
DBPack SQL Tracing 功能及数据加密功能详解
我在启牛开户安全吗?谁能告诉我开不靠谱?
极化微波成像概述2
使用设备树时对应的驱动编程
QPalette调色板、框架色彩填充
2022年SQL经典面试题总结(带解析)
完美指南|如何使用 ODBC 进行无代理 Oracle 数据库监控?
一加OnePlus 10RT出现在Geekbench上 产品发布似乎也已临近
CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) Solution
typora操作手册








