36. Valid Sudoku
来源:互联网 发布:windows和mac内核区别 编辑:程序博客网 时间:2024/06/07 04:12
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.
2015年6月8日
一单某一行中、某一列中或某一个小9宫格中有一个数字重复出现,则该解法为非法。
class Solution {public: bool isValidSudoku(vector<vector<char>>& board) { //for every row,creat a hash table for number 1~9(actually 0~8), //1 denotes a number has been used,0 denotes a number has not been used int rowHashTable[9][9]={0}; //the same as rowHashTable int colHashTable[9][9]={0}; //the same as rowHashTable int boxHashTable[9][9]={0}; for(int i = 0;i<board.size();++i) { for(int j = 0;j<board[0].size();++j) { //only handle non . cells if(board[i][j] != '.') { int num = board[i][j] - '0' - 1; int boxNum = i / 3 * 3+j / 3; if(rowHashTable[i][num] || colHashTable[j][num] || boxHashTable[boxNum][num]) return false; rowHashTable[i][num] = colHashTable[j][num] = boxHashTable[boxNum][num] = 1; } } } return true; }};
验证每一个9宫格中的数字是否合法是这个题的关键
class Solution {public: bool isValidSudoku(vector<vector<char> > &board) { if(isValidRow(board) && isValidCol(board) && isValidBox(board)) return true; else return false; } //通过计数,来判定某一个数是否合法 //不管是在行还是在列还是在9宫格中,每一个数字最多只能出现1次 bool isValidNum(int count[],char c) { if(c == '.') return true; else return ++count[c-'1']<=1; } bool isValidRow(vector<vector<char> > &board) { for(int row = 0;row < 9;row++) { int count[9]={0}; for(int col = 0;col < 9;col++) if(!isValidNum(count,board[row][col])) return false; } return true; } bool isValidCol(vector<vector<char> > &board) { for(int col = 0;col < 9;col++) { int count[9]={0}; for(int row = 0;row < 9;row++) if(!isValidNum(count,board[row][col])) return false; } return true; } bool isValidBox(vector<vector<char> > &board) { //存储每个9宫格的中心坐标 int boxCenter[9][2]={{1,1},{1,4},{1,7}, {4,1},{4,4},{4,7}, {7,1},{7,4},{7,7}}; //九宫格中其他格子与中心的偏移量 int shift[9][2]={{-1,-1},{-1,0},{-1,1}, {0,-1},{0,0},{0,1}, {1,-1},{1,0},{1,1}}; for(int i = 0;i<9;i++) { int count[9]={0}; int boxCenterX = boxCenter[i][0]; int boxCenterY = boxCenter[i][1]; for(int j = 0;j<9;j++) { if(!isValidNum(count,board[boxCenterX+shift[j][0]][boxCenterY+shift[j][1]])) return false; } } return true; }};
0 0
- [LeetCode]36.Valid Sudoku
- 36. Valid Sudoku
- LeetCode --- 36. Valid Sudoku
- LeetCode 36.Valid Sudoku
- [Leetcode] 36. Valid Sudoku
- 36. Valid Sudoku
- 【leetcode】36. Valid Sudoku
- 36. Valid Sudoku
- 36. Valid Sudoku
- [leetcode] 36. Valid Sudoku
- Leetcode 36. Valid Sudoku
- 36. Valid Sudoku
- 36. Valid Sudoku LeetCode
- 36. Valid Sudoku
- 36. Valid Sudoku
- 36. Valid Sudoku
- 36. Valid Sudoku
- 36. Valid Sudoku
- COM组件创建过程
- ServletContext
- Android四大组件简述之BroadcastReceiver
- 头文件互相包含的问题
- 5款Windows 界面原型设计工具
- 36. Valid Sudoku
- 红黑树的插入删除-红黑树动态顺序统计
- bombing:cocos2d-x之CCDirector类2
- ASP.NET连接SQL Server数据库
- javaSE-面向对象-final关键字
- POJ 2762 Going from u to v or from v to u? Tarjan缩点+判断链
- VMware虚拟机以NAT方式解决无法上网的问题
- 安卓智能手机运行iFIX组态软件
- hibernate与mysql映射类型对应表与mysql导入导出