当前位置:网站首页>[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)
边栏推荐
- Shutter web hardware keyboard monitoring
- MySQL advanced part 1: index
- CPU内核和逻辑处理器的区别
- Daily question 1688 Number of matches in the competition
- 1.14 - 流水线
- 1041 Be Unique
- One question per day 1765 The highest point in the map
- Sword finger offer II 058: schedule
- MySQL advanced part 1: View
- RGB LED infinite mirror controlled by Arduino
猜你喜欢
![[practical skills] technical management of managers with non-technical background](/img/4d/1081c71df6ee2087359111baf7498a.png)
[practical skills] technical management of managers with non-technical background

1.14 - 流水线

redis发布订阅命令行实现
![Introduction to LVS [unfinished (semi-finished products)]](/img/72/d5a943a8d6d71823dcbd7f23dda35b.png)
Introduction to LVS [unfinished (semi-finished products)]

中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章

Traditional databases are gradually "difficult to adapt", and cloud native databases stand out

LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively

How to adjust bugs in general projects ----- take you through the whole process by hand

SQLMAP使用教程(一)

Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
随机推荐
Basic explanation of typescript
传统数据库逐渐“难适应”,云原生数据库脱颖而出
Open source storage is so popular, why do we insist on self-development?
【Rust 笔记】13-迭代器(中)
leetcode-6111:螺旋矩阵 IV
Redis publish subscribe command line implementation
TypeScript 基础讲解
Collection: programming related websites and books
[rust notes] 17 concurrent (Part 1)
liunx启动redis
2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
Overview of variable resistors - structure, operation and different applications
Binary search template
MySQL advanced part 2: optimizing SQL steps
Data visualization chart summary (II)
Daily question 2006 Number of pairs whose absolute value of difference is k
Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
MySQL advanced part 1: triggers
【Rust 笔记】14-集合(下)
wordpress切换页面,域名变回了IP地址