[LeetCode]Valid Sudoku
2015-04-18 13:07
330 查看
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
LeetCode Source
分析:利用map来判断
Brute Force解法。
1)每一行是否有重复的数;
2)每一列是否有重复的数;
3)每一个9宫是否有重复的数。
可以利用Map来判断是否重复。
The Sudoku board could be partially filled, where empty cells are filled with the character
'.'.
LeetCode Source
分析:利用map来判断
Brute Force解法。
1)每一行是否有重复的数;
2)每一列是否有重复的数;
3)每一个9宫是否有重复的数。
可以利用Map来判断是否重复。
class Solution { public: bool isValidSudoku(vector<vector<char> > &board) { vector<map<char,int>> Sudoku(27); for(int i=0;i<9;++i){ //行相等判断 for(int j=0;j<9;++j){ if(board[i][j]=='.') continue; if(board[i][j]>'9'||board[i][j]<'0') return false; if((Sudoku[i])[board[i][j]]>0) return false; (Sudoku[i])[board[i][j]]++; } } for(int i=0;i<9;++i){//列相等判断 for(int j=0;j<9;++j){ if(board[j][i]=='.') continue; if((Sudoku[i+9])[board[j][i]]>0) return false; (Sudoku[i+9])[board[j][i]]++; } } int it=0; //每个单元格相等判断 while(it<9){//迭代9次 for(int i=(it%3)*3;i<(it%3)*3+3;++i){ for(int j=(it/3)*3;j<(it/3)*3+3;++j){ if(board[i][j]=='.') continue; if((Sudoku[it+18])[board[i][j]]>0) return false; (Sudoku[it+18])[board[i][j]]++; } } ++it; } return true; } };
相关文章推荐
- [leetcode] Valid Sudoku
- 【leetcode】【36】Valid Sudoku
- LeetCode - 36. Valid Sudoku
- 【LeetCode-面试算法经典-Java实现】【036-Valid Sudoku(验证数独棋盘)】
- LeetCode 36. Valid Sudoku(数独Ⅰ)
- Leetcode:Valid Sudoku
- leetcode笔记:Valid Sudoku
- Leetcode--Valid Sudoku
- Leetcode: Valid Sudoku
- leetcode之Valid Sudoku
- [leetcode]36题 Valid Sudoku的JavaScript解法
- LeetCode 36 Valid Sudoku(有效数独)
- Leetcode 36 Valid Sudoku
- [LeetCode]Valid Sudoku解题记录
- [leetcode]Valid Sudoku(判断有效数独 C语言实现)
- 【E】【59】【leetcode题解】Valid Sudoku
- LeetCode(36)Valid Sudoku
- LeetCode题解——Valid Sudoku
- leetcode - 36.Valid Sudoku
- Leetcode: Valid Sudoku