当前位置:网站首页>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;
}
}
}
}
边栏推荐
- 关系运算符和if,else语句
- QT_QThread thread
- GTK修改pixmap像素,提取pixmap像素RGB值
- 2022年MySQL最新面试题
- Basic image processing in opencv
- SQL函数 TO_CHAR(三)
- opencv语法Mat类型总结
- When custom annotations implement log printing, specific fields are blocked from printing
- Are online account opening commissions reliable? Is online account opening safe?
- 极化微波成像概述2
猜你喜欢
随机推荐
【Day_08 0426】求最小公倍数
opencv基本的图像处理
8月微软技术课程,欢迎参与
面经汇总-社招-6年
Leetcode72. 编辑距离
MySql 怎么查出符合条件的最新的数据行?
2022年SQL大厂高频实战面试题(详细解析)
XAML WPF item groupBox control
RecSys'22|CARCA: Cross-Attention-Aware Context and Attribute Recommendations
浅谈大数据背景下数据库安全保障体系
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
【报错】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat‘)
后台管理系统的权限思路
B002 - Embedded Elderly Positioning Tracking Monitor
QT基础功能,信号、槽
深入分析类加载器
浅谈游戏音效测试点
小贝拉机器人是朋友_普渡科技召开新品发布会,新一代送餐机器人“贝拉”温暖登场...
QT_QThread thread
【Error】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat’)









