当前位置:网站首页>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;
}
};
边栏推荐
- Data standardization processing
- [learning notes] factor analysis
- How to understand the usability of cloud native databases?
- Use of WC command
- 2022 t elevator repair test question bank simulation test platform operation
- T检验(检验两个总体的均值差异是否显著)
- Lecture 30 linear algebra Lecture 4 linear equations
- with torch.no_grad():的使用原因
- Characters and integers
- No module named ‘PyEMD‘ ;使用plt.figure()TypeError: ‘module‘ object is not callable
猜你喜欢
![return new int[]{i + 1, mid + 1}; return {i + 1, mid + 1};](/img/6a/45a4494276deba72ef9833818229f5.png)
return new int[]{i + 1, mid + 1}; return {i + 1, mid + 1};

Leetcode 36. 有效的数独(可以,一次过)

Bluecmsv1.6 code audit

30讲 线性代数 第四讲 线性方程组

2022 P cylinder filling test exercises and online simulation test
![[go language questions] go from 0 to entry 5: comprehensive review of map, conditional sentences and circular sentences](/img/7a/16b481753d7d57f50dc8787eec8a1a.png)
[go language questions] go from 0 to entry 5: comprehensive review of map, conditional sentences and circular sentences

C # application interface development foundation - form control

【毕业季·进击的技术er】努力只能及格,拼命才能优秀!

CNN-LSTM的flatten

Day88.七牛云: 房源图片、用户头像上传
随机推荐
方 差 分 析
iterator中的next()为什么要强转?
管道 | 与重定向 >
Software supply chain security risk guide for enterprise digitalization and it executives
Are you still paying for your thesis? Come and join me
ROS中quaternion四元数和欧拉角转换
Rsync remote synchronization
03.hello_ rust
关键字long
QSP read label configuration error
圆球等的相关计算
Markdown mermaid種草(1)_ mermaid簡介
[learning notes] cluster analysis
Markdown Mermaid planting grass (1)_ Introduction to Mermaid
计网 | 一文解析TCP协议所有知识点
输入分隔符
Day88. qiniu cloud: upload house source pictures and user avatars
Number theory -- detailed proof of Euler function, sieve method for Euler function, Euler theorem and Fermat theorem
Grep text search tool
Characters and integers