当前位置:网站首页>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;
}
}
}
}
边栏推荐
猜你喜欢
随机推荐
【翻译】CNCF培养的OpenMetrics成为一个孵化项目
RecSys'22|CARCA:交叉注意力感知上下文和属性进行推荐
QLineEdit learning and use
极化微波成像概述2
QT_QDialog dialog
B001 - 基于STM32的智能生态鱼缸
云原生全景图详解
吴恩达机器学习课后习题——kmeans
【Day_11 0506】 最近公共祖先
matlab 基于奇偶校验的LSB隐藏水印 三种改进
小贝拉机器人是朋友_普渡科技召开新品发布会,新一代送餐机器人“贝拉”温暖登场...
极化微波成像概述3
opencv实时人脸检测
【Day_08 0426】两种排序方法
【无标题】setInterval和setTimeout详解
golang json returns null
一加OnePlus 10RT出现在Geekbench上 产品发布似乎也已临近
B002 - 基于嵌入式的老人定位追踪监测仪
数字化采购管理系统开发:精细化采购业务流程管理,赋能企业实现“阳光采购”
面经汇总-社招-6年









