LeetCode *** 36. Valid Sudoku
2016-04-09 20:36
232 查看
题目:
Determine if a Sudoku is valid, according to:
Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character
![](http://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
分析:
因为只是判断出现的数字是否构成一个有效的数独,那么直接判断横竖以及小正方形内的数字即可。
代码:
Determine if a Sudoku is valid, according to:
Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character
'.'.
![](http://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
分析:
因为只是判断出现的数字是否构成一个有效的数独,那么直接判断横竖以及小正方形内的数字即可。
代码:
class Solution { public: bool isValidSudoku(vector<vector<char>>& board) { int sudoku[9][9]; for(int i=0;i<9;++i) for(int j=0;j<9;++j) if(board[i][j]!='.')sudoku[i][j]=board[i][j]-'0'; else sudoku[i][j]=0; bool Hflag[9]={}; bool Vflag[9]={}; bool Mflag[9]={}; for(int i=0;i<9;++i){ for(int j=0;j<9;++j){ if(sudoku[i][j]){ if(Hflag[sudoku[i][j]-1])return false; else Hflag[sudoku[i][j]-1]=1; } if(sudoku[j][i]){ if(Vflag[sudoku[j][i]-1])return false; else Vflag[sudoku[j][i]-1]=1; } if(sudoku[(i/3)*3+j/3][(i%3)*3+j%3]){ if(Mflag[sudoku[(i/3)*3+j/3][(i%3)*3+j%3]-1])return false; else Mflag[sudoku[(i/3)*3+j/3][(i%3)*3+j%3]-1]=1; } } for(int j=0;j<9;++j){ Hflag[j]=0; Vflag[j]=0; Mflag[j]=0; } } return true; } };
相关文章推荐
- iOS定时器使用小结
- 【hihoCoder】1041. 国庆出游
- 结对编程之黄金分割
- 排名机制
- iOS 音频播放
- 黑板客爬虫闯关第二关
- Java类的加载和类加载器
- 把activity传递给FragmentActivity的数据再传递给FragmentActivity的子Fragment
- 管线分布竣工图(水、强电、弱电、结构)
- 从一个深度图里面导出NARF特征
- JavaScript学习(三)
- CSS换行:word-wrap、word-break和text-wrap差别
- OpenGL学习之路(一)
- O(N)最长回文子串算法——Manacher算法
- ubuntu 下 Qt 编译出现 cannot find -lGL错误
- 谷歌浏览器常用插件
- Java注解
- 快速排序
- C++ STL pair
- 第六周作业:UML在软件开发过程中的作用