Leetcode# 36 Valid Sudoku
2015-07-11 21:29
183 查看
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.
Difficulty:Easy
遍历就好了,没什么难的。
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.
Difficulty:Easy
遍历就好了,没什么难的。
bool isValidSudoku(vector<vector<char> >& board) { int row[9],line[9],sq[9]; int temp; for(int i = 0;i<9;i++) { memset(row,0,sizeof(row)); for(int j = 0;j<9;j++) { if(board[i][j]=='.') continue; temp = board[i][j]-'0'; row[temp-1]++; } for(int j = 0;j<9;j++) { if(row[j]>1) return false; } } for(int i = 0;i<9;i++) { memset(line,0,sizeof(line)); for(int j = 0;j<9;j++) { if(board[j][i]=='.') continue; temp = board[j][i]-'0'; line[temp-1]++; } for(int j = 0;j<9;j++) { if(line[j]>1) return false; } } int a = 0, b = 0,cou=0; while(cou<9) {memset(sq,0,sizeof(sq)); switch(cou){ case 0: a=0;b=0; break; case 1: a=0;b=3; break; case 2: a=0;b=6; break; case 3: a=3;b=0; break; case 4: a=3;b=3; break; case 5: a=3;b=6; break; case 6: a=6;b=0; break; case 7: a=6;b=3; break; case 8: a=6;b=6; break; } for(int i=a;i<a+3;i++) { for(int j=b;j<b+3;j++) { if(board[i][j]=='.') continue; temp = board[i][j]-'0'; sq[temp-1]++; } } for(int j = 0;j<9;j++) { if(sq[j]>1) return false; } cou++; } return true; }
相关文章推荐
- Android: NDK编程入门笔记
- Dreamweaver cs6合理配色设置字体大小以提高代码阅读性
- Hiho 1014 题目
- 阅读摘要
- 【栈的应用】栈的出栈序列问题研究
- HTTP::Request
- 1028. 人口普查(20)
- SublimeText3: ImportError: No module named ‘urllib2′ 的解决办法
- 类与对象
- 二叉树的实现
- [LintCode] 最多有多少个点在一条直线上
- maven setting.xml配置说明
- 欢迎使用CSDN-markdown编辑器
- HDU 5280 BestCoder Round #47 1001:Senior's Array
- 1027. 打印沙漏(20)
- Lowest Common Ancestor of a Binary Search Tree
- Ubuntu 开机后总是提示“system program problem detected”
- 【转】Install SmartGit via PPA in Ubuntu 13.10/13.04/12.04/Linux Mint
- 项目开发之UML之初识
- 【C#】【Thread】Monitor和Lock