当前位置:网站首页>【LeetCode】Day95-有效的数独&矩阵置零
【LeetCode】Day95-有效的数独&矩阵置零
2022-07-05 06:13:00 【倒过来是圈圈】
题目1
题解
如果按照规则行、列、3*3矩阵挨个试的话,工作量巨大…所以怎么才能降低复杂度呢?是这道题要重点考虑的问题
可以使用哈希表记录每一行、每一列和每一个小九宫格中,每个数字出现的次数。只需要遍历数独一次,在遍历的过程中更新哈希表中的计数,并判断是否满足有效的数独的条件即可。
class Solution {
public boolean isValidSudoku(char[][] board) {
int rows[][]=new int[9][9];//行
int columns[][]=new int[9][9];//列
int three[][][]=new int[3][3][9];//小九宫格
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;//字符转换为数字
rows[i][index]++;
columns[index][j]++;
three[i/3][j/3][index]++;//注意怎么表示小九宫格的哈希表
if(rows[i][index]>1||columns[index][j]>1||three[i/3][j/3][index]>1)
return false;
}
}
}
return true;
}
}
时间复杂度: O ( 1 ) O(1) O(1),数独大小是固定的,因此复杂度为O(1)
空间复杂度: O ( 1 ) O(1) O(1),由于数独大小固定,因此哈希表大小也固定,复杂度为O(1)
题目2
题解
使用两个数组row[]和column[]标记哪行哪列有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++){
//元素为0,行列打标记
if(matrix[i][j]==0){
row[i]=true;
column[j]=true;
}
}
}
//根据标记,行列赋零
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;
}
}
}
}
时间复杂度: O ( m n ) O(mn) O(mn)
空间复杂度: O ( m + n ) O(m+n) O(m+n)
边栏推荐
- Collection: programming related websites and books
- Introduction to LVS [unfinished (semi-finished products)]
- 2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
- 数据可视化图表总结(一)
- leetcode-9:回文数
- Smart construction site "hydropower energy consumption online monitoring system"
- Regulations for network security events of vocational group in 2022 Guizhou Vocational College skill competition
- Dynamic planning solution ideas and summary (30000 words)
- Sqlmap tutorial (1)
- wordpress切换页面,域名变回了IP地址
猜你喜欢

LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树

Full Permutation Code (recursive writing)

Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135

Overview of variable resistors - structure, operation and different applications

Appium自动化测试基础 — Appium测试环境搭建总结

On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech

QQ电脑版取消转义符输入表情

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

The connection and solution between the shortest Hamilton path and the traveling salesman problem

Data visualization chart summary (II)
随机推荐
Bit mask of bit operation
Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
redis发布订阅命令行实现
leetcode-6110:网格图中递增路径的数目
Individual game 12
【Rust 笔记】15-字符串与文本(上)
【Rust 笔记】17-并发(下)
Appium automation test foundation - Summary of appium test environment construction
Wazuh開源主機安全解决方案的簡介與使用體驗
1.14 - 流水线
Daily question 2006 Number of pairs whose absolute value of difference is k
[rust notes] 14 set (Part 1)
Leetcode-3: Longest substring without repeated characters
开源存储这么香,为何我们还要坚持自研?
PC register
Binary search template
One question per day 1447 Simplest fraction
【Rust 笔记】13-迭代器(中)
Dynamic planning solution ideas and summary (30000 words)
Multi screen computer screenshots will cut off multiple screens, not only the current screen