当前位置:网站首页>Leetcode 36. Effective Sudoku (yes, once)
Leetcode 36. Effective Sudoku (yes, once)
2022-06-28 20:21:00 【I'm not xiaohaiwa~~~~】

Please judge a 9 x 9 Is the Sudoku effective . It only needs According to the following rules , Verify that the numbers you have filled are valid .
- Numbers 1-9 Only once in a row .
- Numbers 1-9 It can only appear once in each column .
- Numbers 1-9 Separated by thick solid lines in each 3x3 Only once in the palace .( Please refer to the example figure )
Be careful :
- An effective Sudoku ( Part has been filled in ) Not necessarily solvable .
- Just follow the above rules , Verify that the numbers you have filled are valid .
- Blank space ‘.’ Express .
Example 1:

Input :board =
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output :true
Example 2:
Input :board =
[["8","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output :false
explain : Except for the first number in the first line from 5 Change it to 8 outside , The other numbers in the space are the same as Example 1 identical . But because of the 3x3 There are two in the palace 8 There is , So this Sudoku is invalid .
Tips :
- board.length == 9
- board[i].length == 9
- board[i][j] It's a number (1-9) perhaps ‘.’
Main idea :map
Code:
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
map<char,int>mymap;
pair<map<char, int>::iterator, bool> ret;
for(int j=0;j<board[0].size();j++)
{
for(int i=0;i<board.size();i++)
{
vector<char>sub=board[i];
if(board[i][j]=='.')
continue;
ret=mymap.insert(pair<char,int>(board[i][j],0));
if(!ret.second)
return false;
cout<<board[i][j]<<endl;
}
mymap.clear();
}
mymap.clear();
for(int i=0;i<board.size();i++)
{
vector<char>sub=board[i];
for(int j=0;j<sub.size();j++)
{
if(sub[j]=='.')
continue;
ret=mymap.insert(pair<char,int>(sub[j],0));
if(!ret.second)
return false;
}
mymap.clear();
}
mymap.clear();
cout<<"++++"<<endl;
int row=0;
int col=0;
do
{
for(int i=row;i<row+3;i++)
{
vector<char>sub=board[i];
for(int j=col;j<col+3;j++)
{
if(sub[j]=='.')
continue;
ret=mymap.insert(pair<char,int>(sub[j],0));
if(!ret.second)
return false;
}
}
col+=3;
mymap.clear();
if(col==9)
{
col=0;
row+=3;
cout<<"row="<<row<<endl;
}
if(row==9)
{
break;
}
}while(1);
return true;
}
};
边栏推荐
猜你喜欢

【Try to Hack】Cobalt Strike(一)

ArrayList of collection
oracle delete误删除表数据后如何恢复

Number theory -- detailed proof of Euler function, sieve method for Euler function, Euler theorem and Fermat theorem

ThreadLocal原理

学习太极创客 — MQTT 第二章(七)ESP8266 MQTT 遗嘱应用

Racher add / delete node

Markdown mermaid種草(1)_ mermaid簡介

Severity code description project file line suppress status error lnk2038 detected a mismatch of "runtimelibrary": the value "md\u dynamicrelease" does not match the value "mdd\u dynamicde"

ThreadLocal principle
随机推荐
Are you still paying for your thesis? Come and join me
Risc-v instruction set
iterator中的next()为什么要强转?
电子科大(申恒涛团队)&京东AI(梅涛团队)提出用于视频问答的结构化双流注意网络,性能SOTA!优于基于双视频表示的方法!...
Quaternion quaternion and Euler angle transformation in ROS
Rsync remote synchronization
T检验(检验两个总体的均值差异是否显著)
Average score of 100 people
2022 t elevator repair test question bank simulation test platform operation
Various types of long
SQL server2019 create a new SQL server authentication user name and log in
2. 整合 Filter
市值1200亿美金,老牌财税巨头Intuit是如何做到的?
Data standardization processing
How to "calculate" in the age of computing power? The first mover advantage of "convergence of computing and networking" is very important!
Lucene构建索引的原理及源代码分析
redisTemplate
T-test (test whether the mean difference between the two populations is significant)
Pyinstaller打包pikepdf失败的问题排查
A few lines of code can realize complex excel import and export. This tool class is really powerful!