LeetCode 036 Valid Sudoku
2015-11-10 09:35
357 查看
题目描述
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 ‘.’.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
代码
[code] public static boolean isValidSudoku(char[][] board) { if (board == null || board.length != 9 || board[0].length != 9) { return false; } int mx = board.length; int my = board[0].length; // row for (int x = 0; x < mx; x++) { boolean[] flag = new boolean[10]; for (int y = 0; y < my; y++) { char c = board[x][y]; if (c != '.') { if (flag[c - '0'] == false) { flag[c - '0'] = true; } else { return false; } } } } // column for (int y = 0; y < my; y++) { boolean[] flag = new boolean[10]; for (int x = 0; x < mx; x++) { char c = board[x][y]; if (c != '.') { if (flag[c - '0'] == false) { flag[c - '0'] = true; } else { return false; } } } } // square for (int x = 0; x < mx / 3; x++) { for (int y = 0; y < my / 3; y++) { boolean[] flag = new boolean[10]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { char c = board[x * 3 + i][y * 3 + j]; if (c != '.') { if (flag[c - '0'] == false) { flag[c - '0'] = true; } else { return false; } } } } } } return true; }
相关文章推荐
- 一个极好的关于画图的网站
- 线程同步 旋转锁 Interlocked 用户模式同步对象 InterlockedExchange
- 图片圆形加阴影效果
- POJ1321棋盘问题(AC2)
- Spring Security3 - MVC 整合教程 (初识Spring Security3)
- vc 与 js应用
- Google APAC 2016 University Graduates Test Round D
- Merge k Sorted Lists-Leetcode
- 【学习OpenCV】flip函数、镜像操作
- Java 单例模式详解
- MFC 简单封装word, office操作。
- Android Studio 1.4带来的福利
- Facebook如何向十亿人推荐东西
- Objective-C学习—UIScrollView控件使用
- iOS开发——UI组件(个人整理)
- WordPress 函数调用大全
- Hadoop-4
- Sed在匹配行前后加入一行
- iOS之CocoaPods使用步骤
- junit中before和beforeclass区别