当前位置:网站首页>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;
}
}
}
}
边栏推荐
猜你喜欢
Xingtu has been short of disruptive products?Will this M38T from the Qingdao factory be a breakthrough?
MySQL 45 Talk | 09 How to choose common index and unique index?
实现mnist手写数字识别
LeaRun.net快速开发动态表单
星途一直缺颠覆性产品?青岛工厂这款M38T,会是个突破点?
Detailed explanation of the working principle of crystal oscillator
频域分析实践介绍
关于Mysql服务无法启动的问题
【Day_09 0427】走方格的方案数
opencv syntax Mat type summary
随机推荐
gtk显示4通道rgba图像
缓存一致性MESI与内存屏障
星途一直缺颠覆性产品?青岛工厂这款M38T,会是个突破点?
广汽埃安“弹匣电池”,四大核心技术,出行安全保障
SQL函数 TO_CHAR(一)
【Day_11 0506】 最近公共祖先
粒子滤波 particle filter —从贝叶斯滤波到粒子滤波——Part-I(贝叶斯滤波)
关于MySql中explain结果filtered的理解
sql添加索引
When custom annotations implement log printing, specific fields are blocked from printing
面经汇总-社招-6年
顺序表的简单描述及代码的简单实现
【TDP加码福利】COS用户实践征文月,等你来投稿!!!
BITS Pilani|SAC-AP:基于 Soft Actor Critic 的深度强化学习用于警报优先级
中信证券是国内十大券商吗?怎么开户安全?
我在启牛开户安全吗?谁能告诉我开不靠谱?
使用设备树时对应的驱动编程
吴恩达机器学习课后习题——kmeans
机器学习快速入门
OnePlus 10RT appears on Geekbench, product launch also seems to be approaching