当前位置:网站首页>[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)
边栏推荐
- 1.14 - 流水线
- Leetcode-6109: number of people who know secrets
- Appium automation test foundation - Summary of appium test environment construction
- Dynamic planning solution ideas and summary (30000 words)
- Leetcode-6110: number of incremental paths in the grid graph
- Sqlmap tutorial (1)
- [cloud native] record of feign custom configuration of microservices
- wordpress切换页面,域名变回了IP地址
- Appium基础 — 使用Appium的第一个Demo
- One question per day 1447 Simplest fraction
猜你喜欢
随机推荐
1040 Longest Symmetric String
Daily question 2013 Detect square
1.14 - assembly line
QQ computer version cancels escape character input expression
LeetCode 1200. Minimum absolute difference
开源存储这么香,为何我们还要坚持自研?
Appium自动化测试基础 — Appium测试环境搭建总结
Liunx starts redis
【Rust 笔记】15-字符串与文本(上)
Usage scenarios of golang context
Leetcode-6109: number of people who know secrets
Daily question 1189 Maximum number of "balloons"
MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!
leetcode-6108:解密消息
[rust notes] 16 input and output (Part 2)
Règlement sur la sécurité des réseaux dans les écoles professionnelles secondaires du concours de compétences des écoles professionnelles de la province de Guizhou en 2022
MySQL advanced part 2: optimizing SQL steps
Quickly use Amazon memorydb and build your own redis memory database
QQ电脑版取消转义符输入表情
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库