题目:

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.

题解:

Solution 1

class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
const int n = ; for(int i = ; i < n; ++i){
bool used[n] = {false};
for(int j = ; j < n; ++j){
if(isused(board[i][j], used))
return false;
}
}
for(int i = ; i < n; ++i){
bool used[n] = {false};
for(int j = ; j < n; ++j){
if(isused(board[j][i], used))
return false;
}
}
for(int r = ; r < ; ++r){
for(int c = ; c < ; ++c){
bool used[n] = {false};
for(int i = r * ; i < r * + ; ++i){
for(int j = c * ; j < c * + ; ++j){
if(isused(board[i][j], used))
return false;
}
}
}
}
return true;
}
bool isused(char val, bool used[]){
if(val == '.')
return false;
if(used[val - ''])
return true;
used[val - ''] = true;
return false;
}
};

Solution 2

class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
const int n = ;
vector<vector<bool>> rowboard(n, vector<bool>(n, false));
vector<vector<bool>> colboard(n, vector<bool>(n, false));
vector<vector<bool>> celboard(n, vector<bool>(n, false));
for(int i = ; i < n; ++i){
for(int j = ; j < n; ++j){
if(board[i][j] == '.') continue;
int ch = board[i][j] - '';
if(rowboard[i][ch] || colboard[ch][j] || celboard[ * (i / ) + j / ][ch])
return false;
rowboard[i][ch] = true;
colboard[ch][j] = true;
celboard[ * (i / ) + j / ][ch] = true;
}
}
return true;
}
};

最新文章

  1. Linux下安装性能测试负载机LG
  2. Java:对象的强、软、弱和虚引用
  3. 众安「尊享e生」果真牛的不可一世么?
  4. php 工厂模式
  5. nsstring字符串重组
  6. YUI Reset CSS (学习摘抄)
  7. Laravel 安装predis 扩展
  8. java环境中基于jvm的两大语言:scala,groovy
  9. 从源码看java中Integer的缓存问题
  10. PHP得出附件扩展名
  11. A题进行时--浙大PAT 1001-1010
  12. sap判断条件
  13. ASP.NET Web Service中使用Session 及 Session丢失解决方法 续
  14. 关于node的基础理论,书上看来的
  15. Java开发笔记(六十七)清单:ArrayList和LinkedList
  16. .net 委托多线程 实时更新界面
  17. pythonのdjango CSRF简单使用
  18. ubuntu14.04安装opencv3.1
  19. Solidity-让合约地址 接受ETH的转账充值的 三种方式
  20. 【转】【JLINK下载失败,STLINK下载失败万能解决方案】JLINK和STLINK都无法下载时的解决办法,此时芯片并没有报废

热门文章

  1. swagger api 文档框架
  2. 【BZOJ2337】[HNOI2011]XOR和路径 期望DP+高斯消元
  3. hdu 2108 Shape of HDU【判断多边形是否是凸多边形模板】
  4. Dajngo admin使用
  5. python数据分析之:时间序列二
  6. Linux开启防火墙后,设置允许通过的端口
  7. QT5使用Webkti
  8. WebApp页面开发小结
  9. easy_install和pip的安装及使用
  10. SpringBoot学习笔记(2):引入Spring Security