[LeetCode]36. Valid Sudoku
来源:互联网 发布:我淘宝买的东西怎投诉 编辑:程序博客网 时间:2024/06/10 04:57
36. Valid Sudoku
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 ‘.’.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
分析
这个题目没有什么太多的技巧啦,逐行扫描各行,用一个几个set来记录当前行出现的数字;同样方式扫描各列;然后扫描各个方块。
源码
class Solution {public: bool isValidSudoku(vector<vector<char>>& board) { for(int row = 0; row < board.size(); row++) { unordered_set<char> rowSet; unordered_set<char> colSet; for(int col = 0; col < board[row].size(); col++) { // 同一行检查 if(board[row][col] != '.' && rowSet.find(board[row][col]) != rowSet.end()) return false; else rowSet.insert(board[row][col]); // 同一列检查 if(board[col][row] != '.' && colSet.find(board[col][row]) != colSet.end()) return false; else colSet.insert(board[col][row]); } } // 检查正方形 for(int index = 0; index < 9; index++) { unordered_set<char> set; for(int row = 0; row < 3; row++) { for(int col = 0; col < 3; col++) { char c = board[3 * (index / 3) + row][3 * (index % 3) + col]; if(c != '.' && set.find(c) != set.end()) return false; else set.insert(c); } } } return true; }};
0 0
- [LeetCode]36.Valid Sudoku
- LeetCode --- 36. Valid Sudoku
- LeetCode 36.Valid Sudoku
- [Leetcode] 36. Valid Sudoku
- 【leetcode】36. Valid Sudoku
- [leetcode] 36. Valid Sudoku
- Leetcode 36. Valid Sudoku
- 36. Valid Sudoku LeetCode
- leetcode 36. Valid Sudoku
- LeetCode *** 36. Valid Sudoku
- leetcode 36. Valid Sudoku
- [LeetCode]36. Valid Sudoku
- LeetCode 36. Valid Sudoku
- leetcode-36. Valid Sudoku
- 36.[Leetcode]Valid Sudoku
- Leetcode 36.Valid Sudoku
- LeetCode-36.Valid Sudoku
- leetcode 36. Valid Sudoku
- volatile、static、register、extern、auto和const
- Python异步通信模块asynchat
- 利用IPV6实现宿舍远程连接实验室并免费上网
- LeetCode *** 334. Increasing Triplet Subsequence
- DBGridEh-在指示列显示复选框:gioShowRowselCheckboxesEh、dgMultiSelect
- [LeetCode]36. Valid Sudoku
- http://www.cnblogs.com/armyfai/p/3985660.html svn教程
- OpenCV特征点检测匹配图像-----添加包围盒
- static关键字的用法
- AngularJS_4 Dependency Injection(依赖注入)
- 业务概念—类图
- Tomcat调优
- 句柄、引用、指针与对象
- java集合框架08——HashMap和源码分析