LeetCode 36 - Valid Sudoku
2016-03-01 16:41
344 查看
Valid Sudoku
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.
My Code
class Solution { public: bool checkCells(vector<char>& cells) { vector<int> count(9 , 0); for (int j = 0; j < 9; j++) { if (cells[j] == '.') continue; int number = cells[j]-'1'; count[number]++; if (count[number] > 1) return false; } return true; } bool isValidSudoku(vector<vector<char> >& board) { vector<char> cells; // Check row for (int i = 0; i < 9; i++) { if (checkCells(board[i]) == false) return false; } // Check column for (int i = 0; i < 9; i++) { cells.clear(); for (int j = 0; j < 9; j++) cells.push_back(board[j][i]); if (checkCells(cells) == false) return false; } // Check box for (int i = 0; i < 9; i += 3) for (int j = 0; j < 9 ;j += 3) { cells.clear(); for (int ii = i; ii < i + 3; ii++) for (int jj = j; jj < j + 3; jj++) { cells.push_back(board[ii][jj]); } if (checkCells(cells) == false) return false; } return true; } };Runtime: 16 ms
Test Case
5 3 . . 7 . . . .6 . . 1 9 5 . . .
. 9 8 . . . . 6 .
8 . . . 6 . . . 3
4 . . 8 . 3 . . 1
7 . . . 2 . . . 6
. 6 . . . . 2 8 .
. . . 4 1 9 . . 5
. . . . 8 . . 7 9
True
相关文章推荐
- Android 日常开发总结的技术经验 60 条
- 获取Android手机短信中心号码
- 浏览器JS文件缓存问题
- POJ 1016 Numbers That Count(水~)
- 字符串x2+些许作业
- 我的软件工程课目标
- JAVA WEB学习——tomcat服务器首页配置
- 蓝桥杯五2切面条递推公式
- 1901: Zju2112 Dynamic Rankings 整体二分
- .NET AJAX进行跨域调用 MVC 或WEB API
- redis持久化RDB和AOF
- cocos2d-x3.2中怎样优化Cocos2d-X游戏的内存
- LINQ查询XML
- js中的encodeURIComponent()函数
- TypeScript Function(函数)
- 整理 javaweb 连接数据库方法
- GCD介绍
- BZOJ 3698: XWW的难题|有上下界的网络流之最大流
- VBA教程初级(二):数据结构
- spring 对于hibernate操作的封装 HibernateCallback接口的学习