[leetcode] 36. Valid Sudoku
来源:互联网 发布:大众软件官网 编辑:程序博客网 时间:2024/05/17 21:52
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 '.'
.
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.
解法一:
数独的规则就是行、列、9个3*3的cell中不能出现重复的数字。思路是创建三个矩阵,row, col, cell, row[i][c]表示第i行中出现了数字c, col[j][c] 表示第j列出现了数字c, cell[k][c]表示第k个cell出现了数字c。每次去检查 row, col, cell中对应元素是不是有一个为true。
class Solution {public: bool isValidSudoku(vector<vector<char>>& board) { // check size if (board.size()!=9) return false; ////// vector<vector<bool> > row_flag(9,vector<bool>(9,false)); vector<vector<bool> > col_flag(9,vector<bool>(9,false)); vector<vector<bool> > cell_flag(9,vector<bool>(9,false)); for(int i=0; i<9; i++){ if (board[i].size()!=9) return false; for (int j=0; j<9; j++){ if (!validFill(board[i][j])) return false; if (board[i][j] >= '1' && board[i][j] <= '9'){ int val = board[i][j] - '1'; if (row_flag[i][val] || col_flag[j][val] || cell_flag[3*(i/3)+j/3][val]) return false; row_flag[i][val] = true; col_flag[j][val] = true; cell_flag[3*(i/3)+j/3][val] = true; } } } return true; } bool validFill(char c){ return c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9' || c == '.'; }};
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
- 如何提高逻辑思维的能力?
- javaGUI学习——在面板上画一张图片
- simhash
- LintCode笔记(3)——二叉树的先序遍历
- RecyclerView 多选 删除
- [leetcode] 36. Valid Sudoku
- CSS3-盒模型设计
- sed替换^M为回车
- SQL update已经存在的数据insert新的数据
- The Pilots Brothers' refrigerator
- https小结
- 使用USRP和GNU Radio实现GPS位置伪造
- POJ-3070Fibonacci(矩阵快速幂)
- .gitignore java 版本