当前位置:网站首页>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;
}
}
}
}
边栏推荐
- Solve the problem that MySQL cannot insert Chinese data
- MySQL 45 讲 | 09 普通索引和唯一索引,应该怎么选择?
- hcip第九天
- 【Day_11 0506】求最大连续bit数
- MySQL Lock wait timeout exceeded; try restarting transaction 锁等待
- The anxiety of the post-90s was cured by the vegetable market
- 加州大学|通过图抽象从不同的第三人称视频中进行逆强化学习
- opencv语法Mat类型总结
- 阿里云的域名和ip绑定
- 成为优秀架构师必备技能:怎样才能画出让所有人赞不绝口的系统架构图?秘诀是什么?快来打开这篇文章看看吧!...
猜你喜欢
XAML WPF item groupBox control
QT commonly used global macro definitions
Xingtu has been short of disruptive products?Will this M38T from the Qingdao factory be a breakthrough?
RecSys'22|CARCA:交叉注意力感知上下文和属性进行推荐
QPalette palette, frame color fill
分布式消息队列平滑迁移技术实战
MySQL 45 Talk | 09 How to choose common index and unique index?
B005 – 基于STC8的单片机智能路灯控制系统
Shell nl命令详解(显示行号、读取文件)
面经汇总-社招-6年
随机推荐
吴恩达机器学习课后习题——kmeans
opencv real-time face detection
成都理工大学&电子科技大学|用于强化学习的域自适应状态表示对齐
【Day_12 0507】二进制插入
深入分析类加载器
AIOps智能运维的领跑者擎创科技正式入驻InfoQ 写作社区!
【Day_10 0428】井字棋
BITS Pilani|SAC-AP:基于 Soft Actor Critic 的深度强化学习用于警报优先级
QT_QThread线程
关于LocalDateTime的全局返回时间带“T“的时间格式处理
变量交换;复合赋值;增递减运算符
想做期货,农产品期货怎么炒?波动大么
【翻译】CNCF培养的OpenMetrics成为一个孵化项目
SQL函数 TO_CHAR(二)
Shell nl命令详解(显示行号、读取文件)
M1芯片电脑安装cerebro
Are online account opening commissions reliable? Is online account opening safe?
【100个网络运维工作者必须知道的小知识!】
How can become a good architect necessary skills: painting for all the people praise the system architecture diagram?What is the secret?Quick to open this article and have a look!.
【Day_10 0428】密码强度等级