当前位置:网站首页>[leetcode] day95 effective Sudoku & matrix zeroing
[leetcode] day95 effective Sudoku & matrix zeroing
2022-07-05 06:14:00 【Upside down, it's a circle】
subject 1
36. Effective Sudoku 【 secondary 】
Answer key
If you follow the rule 、 Column 、3*3 If the matrix tries one by one , Huge workload … So how can we reduce the complexity ? This is the key consideration of this problem
Sure Use a hash table to record each row 、 In every column and every little nine palace , The number of times each number appears . It only needs Traverse Sudoku once , Update the count in the hash table during traversal , And judge whether the conditions for effective Sudoku are met .
class Solution {
public boolean isValidSudoku(char[][] board) {
int rows[][]=new int[9][9];// That's ok
int columns[][]=new int[9][9];// Column
int three[][][]=new int[3][3][9];// Xiaojiugong grid
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(board[i][j]!='.'){
int index=board[i][j]-'0'-1;// Character to number
rows[i][index]++;
columns[index][j]++;
three[i/3][j/3][index]++;// Notice how to represent the hash table of the small Jiugong lattice
if(rows[i][index]>1||columns[index][j]>1||three[i/3][j/3][index]>1)
return false;
}
}
}
return true;
}
}
Time complexity : O ( 1 ) O(1) O(1), Sudoku size is fixed , So the complexity is O(1)
Spatial complexity : O ( 1 ) O(1) O(1), Due to the fixed size of Sudoku , Therefore, the hash table size is also fixed , The complexity is O(1)
subject 2
73. Matrix zeroing 【 secondary 】
Answer key
Use two arrays row[] and column[] Mark which row and which column have 0
class Solution {
public void setZeroes(int[][] matrix) {
int m=matrix.length,n=matrix[0].length;
boolean[] row=new boolean[m];
boolean[] column=new boolean[n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
// Element is 0, Mark lines
if(matrix[i][j]==0){
row[i]=true;
column[j]=true;
}
}
}
// According to the mark , Zero row and column
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(row[i]==true||column[j]==true)
matrix[i][j]=0;
}
}
}
}
Time complexity : O ( m n ) O(mn) O(mn)
Spatial complexity : O ( m + n ) O(m+n) O(m+n)
边栏推荐
- Daily question 2013 Detect square
- Doing SQL performance optimization is really eye-catching
- 4. 对象映射 - Mapping.Mapster
- 快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
- Shutter web hardware keyboard monitoring
- MySQL advanced part 2: MySQL architecture
- Wazuh开源主机安全解决方案的简介与使用体验
- [rust notes] 15 string and text (Part 1)
- leetcode-22:括号生成
- 【云原生】微服务之Feign自定义配置的记录
猜你喜欢
4. 对象映射 - Mapping.Mapster
Sqlmap tutorial (II) practical skills I
R language [import and export of dataset]
Quickly use Amazon memorydb and build your own redis memory database
Data visualization chart summary (II)
可变电阻器概述——结构、工作和不同应用
SPI 详解
6. Logistic model
Appium foundation - use the first demo of appium
SPI details
随机推荐
leetcode-1200:最小绝对差
Daily question 2006 Number of pairs whose absolute value of difference is k
[rust notes] 13 iterator (Part 2)
[rust notes] 16 input and output (Part 1)
1.13 - RISC/CISC
js快速将json数据转换为url参数
[rust notes] 14 set (Part 2)
剑指 Offer II 058:日程表
SQLMAP使用教程(一)
MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!
QT判断界面当前点击的按钮和当前鼠标坐标
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
Golang uses context gracefully
Redis publish subscribe command line implementation
[cloud native] record of feign custom configuration of microservices
Daily question 1342 Number of operations to change the number to 0
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
可变电阻器概述——结构、工作和不同应用
实时时钟 (RTC)
[practical skills] technical management of managers with non-technical background