leetcode 36

感觉就是遍历。

保存好状态,就是各行各列还有各分区divide的情况

用数组做。

空间小时间大

class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
int row[9][9]={0},col[9][9]={0},div[9][9]={0};
int temp=0,dnum;
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(board[i][j]!='.'){
temp=board[i][j]-'0'-1;
row[i][temp]++;
col[j][temp]++;
dnum=(i/3)*3+j/3;
div[dnum][temp]++;
if(row[i][temp]==2||col[j][temp]==2||div[dnum][temp]==2)
return false;
}
}
}
return true;
}
};

然后

学到了用unordered_set  unordersd_map加速。

空间大时间小

class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
vector<unordered_set<int>> row(9), col(9), block(9);
for(int i = 0; i < 9; ++ i){
for(int j = 0; j < 9; ++ j){
int bindex = (i / 3)* 3 + j / 3;
char cur = board[i][j];
if(cur == '.') continue;
if(row[i].count(cur) || col[j].count(cur) || block[bindex].count(cur)) return false;
row[i].insert(cur);
col[j].insert(cur);
block[bindex].insert(cur);
}
}
return true;
}
};

leetcode 37

做之前还百度了一下有没有解数独的技巧。。。  OxO

上一道题保存了行列块的状态,这里也可以保留,保留的不再是出现与否而是可用数字

回溯就完事了

class Solution {
public:
vector<unordered_set<int>> row, col, block;
void solveSudoku(vector<vector<char>>& board) {
// 预处理,初始状态
int t=0;
unordered_set<int> tp={1,2,3,4,5,6,7,8,9};
for(int i=0;i<9;i++)
{row.push_back(tp);col.push_back(tp);block.push_back(tp);}
for( int i = 0; i < 9; i++)
for( int j = 0; j < 9; j++)
if( board[i][j] != '.'){
t = board[i][j] - '0';
row[i].erase(t), col[j].erase(t), block[(i/3)*3 + j/3].erase(t);
}
int flag=0;
dfs(board,0,flag);
if(flag==0) //题目说明有唯一解,不会出现
cout<<"wrong"<<endl;
} bool check(vector<vector<char>>& board,int i,int j,int num){
if(row[i].find(num)!= row[i].end()&& col[j].find(num) != col[j].end()
&& block[(i/3)*3 + j/3].find(num) != block[(i/3)*3 + j/3].end())
return true;
return false;
} void dfs(vector<vector<char>>& board,int cnt,int &flag){
if( cnt == 81){
flag = 1;
return ;
}
int i = cnt / 9, j = cnt % 9;
if( board[i][j] == '.'){
for( int k = 1; k < 10; k++)
if(check(board, i, j, k)){
row[i].erase(k), col[j].erase(k), block[(i/3)*3 + j/3].erase(k);
board[i][j] = '0'+k;
dfs(board, cnt+1,flag);
if(flag)
return;
else{
row[i].insert(k), col[j].insert( k), block[(i/3)*3 + j/3].insert(k);
board[i][j] = '.';
}
}
else
continue;
}
else
dfs(board, cnt+1,flag);
return ;
}
};

然后效率很差。。时间和空间上

改成数组

class Solution {
public:
int row[10][10], col[10][10], block[10][10];
void solveSudoku(vector<vector<char>>& board) {
// 预处理,初始状态
int t=0;
for( int i = 0; i < 9; i++)
for( int j = 1; j < 10; j++)
{
row[i][j]=0, col[i][j]=0, block[i][j]=0;
}
for( int i = 0; i < 9; i++)
for( int j = 0; j < 9; j++)
if( board[i][j] != '.'){
t = board[i][j] - '0';
row[i][t]=1, col[j][t]=1, block[(i/3)*3 + j/3][t]=1;
}
int flag=0;
dfs(board,0,flag);
if(flag==0) //题目说明有唯一解,不会出现
cout<<"wrong"<<endl;
} void dfs(vector<vector<char>>& board,int cnt,int &flag){
if( cnt == 81){
flag = 1;
return ;
}
int i = cnt / 9, j = cnt % 9;
if( board[i][j] == '.'){
for( int k = 1; k < 10; k++)
if(row[i][k]==0 && col[j][k]==0 && block[(i/3)*3 + j/3][k]==0){
row[i][k]=1, col[j][k]=1, block[(i/3)*3 + j/3][k]=1;
board[i][j] = '0'+k;
dfs(board, cnt+1,flag);
if(flag)
return;
else{
row[i][k]=0, col[j][k]=0, block[(i/3)*3 + j/3][k]=0;
board[i][j] = '.';
}
}
else
continue;
}
else
dfs(board, cnt+1,flag);
return ;
}
};

我好了

然后,不传引用而是copy可以再节省一点空间

然后剪枝,没考虑 OxO

leetcode 36 有效的数独 哈希表 unordered_set unordersd_map 保存状态 leetcode 37 解数独的更多相关文章

  1. Leetcode之回溯法专题-37. 解数独(Sudoku Solver)

    Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...

  2. Java实现 LeetCode 37 解数独

    37. 解数独 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1-9 在每一个以粗实 ...

  3. [leetcode] 37. 解数独(Java)(dfs,递归,回溯)

    37. 解数独 1A 这个题其实15分钟左右就敲出来并且对了...但是由于我输错了一个数..导致我白白debug一个多小时.. 没啥难度,练递归-dfs的好题 class Solution { pri ...

  4. [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针

    一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...

  5. [LeetCode] Fraction to Recurring Decimal 哈希表

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  6. leetcode刷题-37解数独

    题目 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1-9 在每一列只能出现一次.数字 1-9 在每一个以粗实线分隔的 3x ...

  7. C++ 哈希表 (hashtable) 用于保存简单的数据,及数据查找,数据删除

    /*hashtable.h*/ #include<iostream> #include <string> #include<vector> using namesp ...

  8. Leetcode——37.解数独 [##]

    @author: ZZQ @software: PyCharm @file: leetcode37_solveSudoku.py @time: 2018/11/20 16:41 思路:递归回溯 首先, ...

  9. C#LeetCode刷题-哈希表

    哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串   24.2% 中等 18 四数之和   ...

随机推荐

  1. 阿里巴巴微服务与配置中心技术实践之道 配置推送 ConfigurationManagement ConfigDrivenAnyting

    阿里巴巴微服务与配置中心技术实践之道 原创: 坤宇 InfoQ 2018-02-08 在面向分布式的微服务系统中,如何通过更高效的配置管理方式,帮助微服务系统架构持续"无痛"的演进 ...

  2. OSS与文件系统的对比 文件存储 块存储 对象存储

    基本概念介绍_开发指南_对象存储 OSS-阿里云  https://help.aliyun.com/document_detail/31827.html 强一致性 Object 操作在 OSS 上具有 ...

  3. Language Guide (proto3) | proto3 语言指南(十)映射

    Maps - 映射 如果要创建关联映射作为数据定义的一部分,协议缓冲区提供了一种方便的快捷语法: map<key_type, value_type> map_field = N; -其中k ...

  4. SonarQube+jenkins-自动化持续代码扫描

    SonarQube+jenkins-自动化持续代码扫描 1.SonarQube 1.1 SonarQube介绍 1.1.1 SonarQube 工作流程 1. 2 Docker方式安装SonarQub ...

  5. Spring MVC—数据绑定机制,数据转换,数据格式化配置,数据校验

    Spring MVC数据绑定机制 数据转换 Spring MVC处理JSON 数据格式化配置使用 数据校验 数据校验 Spring MVC数据绑定机制 Spring MVC解析JSON格式的数据: 步 ...

  6. 面向对象编程(UDP协议)

    UDP协议 UDP 是User Datagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open System Interconnection,开放式系统互联) 参考模型中一种无 ...

  7. Linux 调整系统时间偏差

    在使用Linux系统部署项目,有时会出现时间跟当前时间不一致的情况,这个时候需要做些调整: 1.首先删除之前设置的时区 rm -rf /etc/localtime 2.创建上海时区 ln -s /us ...

  8. 菜鸟初学Linux——Ubuntu系统中,用root权限进行复制粘贴操作

    long long ago,积累了一些Linux的小知识,拿出来分享一下,希望以后能够在工作上带来一些帮助. 方法一 第一步:打开终端,在命令行里输入sudo nautilus   第二步:输入你的用 ...

  9. 使用timeout-decorator为python函数任务设置超时时间

    需求背景 在python代码的实现中,假如我们有一个需要执行时间跨度非常大的for循环,如果在中间的某处我们需要定时停止这个函数,而不停止整个程序.那么初步的就可以想到两种方案:第一种方案是我们先预估 ...

  10. ASP.NET Core默认容器实现Controller的属性注入

    仅针对Controller的属性注入: 使用默认容器,不依赖第三方库: 故事背景   闲来无事给项目做优化,发现大多数Controller里面都会用到Logger和AutoMapper,每个Contr ...