博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Valid Sudoku
阅读量:6966 次
发布时间:2019-06-27

本文共 2043 字,大约阅读时间需要 6 分钟。

Determine if a Sudoku is valid, according to: .

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.

 

分析:行、列、3x3 box 分布check

1 class Solution { 2     public: 3         bool m_used[9]; 4         bool isValidSudoku(const vector
>& board) { 5 6 // check the row 7 for (int i = 0; i < 9; ++i) { 8 fill(m_used, m_used + 9, false); 9 for (int j = 0; j < 9; ++j)10 {11 if (!check(board[i][j]))12 return false;13 m_used[board[i][j] - '1'] = true;14 }15 }16 17 // check the column18 for (int i = 0; i < 9; ++i) {19 fill(m_used, m_used + 9, false);20 for (int j = 0; j < 9; ++j)21 { 22 if (!check(board[j][i]))23 return false;24 m_used[board[j][i] - '1'] = true;25 } 26 } 27 28 // check the 3x3 box29 for (int r = 0; r < 3; ++r)30 for( int c = 0; c < 3; ++c)31 {32 fill(m_used, m_used + 9, false);33 for (int i = r * 3; i < r * 3 + 3; ++i)34 for (int j = c * 3; j < c * 3 + 3; ++j)35 {36 if (!check(board[i][j]))37 return false;38 m_used[board[i][j] - '1'] = true;39 }40 }41 return true;42 }43 bool check(char ch) {44 if (ch == '.') return true;45 if (m_used[ch - '1'] == true)46 return false;47 else48 return true;49 }50 };

 

转载地址:http://wbwsl.baihongyu.com/

你可能感兴趣的文章
关于我们
查看>>
jQuery之input
查看>>
下载文件 利用NSURLSession下载文件
查看>>
正则表达式(grep,egrep,fgrep)
查看>>
hadoop-namenode启动过程及坏块处理流程
查看>>
Java虚拟机学习 - 对象引用强度
查看>>
shell 检查文件是否被串改
查看>>
linux文件共享
查看>>
TEC-003-sqlmap安装及使用
查看>>
CentOS6.4_X86_64 安装Drupal-7.31必须成功版!
查看>>
mybatis 调用存储过程,如何抛出sql异常
查看>>
SUN Zone Cluster安装及配置说明之一
查看>>
时序约束
查看>>
使用accordion插件实现页面中多区域的折叠操作8-4
查看>>
IOS开发实现app消息推送
查看>>
30套高质量的图标素材免费下载
查看>>
SAP CLIENT拷贝详细说明
查看>>
跟我学习dubbo-Dubbo监控中心的介绍与简易监控中心的安装(7)
查看>>
phpmyadmin为何要使用mysqli
查看>>
php中3DES加密技术
查看>>