当前位置:网站首页>[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)
边栏推荐
- LaMDA 不可能觉醒吗?
- Appium自动化测试基础 — Appium测试环境搭建总结
- SPI details
- 对for(var i = 0;i < 5;i++) {setTimeout(() => console.log(i),1000)}的深入分析
- Is it impossible for lamda to wake up?
- Leetcode-3: Longest substring without repeated characters
- 【LeetCode】Day94-重塑矩阵
- Wazuh開源主機安全解决方案的簡介與使用體驗
- 1.14 - assembly line
- [rust notes] 14 set (Part 2)
猜你喜欢
Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
Redis publish subscribe command line implementation
MySQL advanced part 2: storage engine
Sqlmap tutorial (II) practical skills I
1.14 - assembly line
数据可视化图表总结(二)
Introduction et expérience de wazuh open source host Security Solution
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
Doing SQL performance optimization is really eye-catching
1.14 - 流水线
随机推荐
Leetcode-556: the next larger element III
One question per day 2047 Number of valid words in the sentence
“磐云杯”中职网络安全技能大赛A模块新题
2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
1039 Course List for Student
[rust notes] 14 set (Part 2)
Real time clock (RTC)
做 SQL 性能优化真是让人干瞪眼
LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
Basic explanation of typescript
【云原生】微服务之Feign自定义配置的记录
可变电阻器概述——结构、工作和不同应用
【LeetCode】Day94-重塑矩阵
The difference between CPU core and logical processor
Leetcode array operation
leetcode-556:下一个更大元素 III
Data visualization chart summary (I)
SPI 详解
LeetCode 1200. Minimum absolute difference
884. Uncommon words in two sentences