判定一个数独是否有效。
来源:互联网 发布:淘宝里猜你喜欢在哪里 编辑:程序博客网 时间:2024/06/14 03:07
/**
* Sudoku
* Created by Administrator on 2017/6/24.
* 请判定一个数独是否有效。
* 该数独可能只填充了部分数字,其中缺少的数字用 . 表示。
*/
class Solution { /** * @param board: the board @return: wether the Sudoku is valid */ private static final int SUDOKU = 9; private static final int BLOCK = 3; public boolean isValidSudoku(char[][] board) { int row; int col; //分割为三个9*3的 int d = 0; char[][] c_block = new char[SUDOKU * 3][SUDOKU]; for (int i = 0; i < SUDOKU; i++) { for (int j = 0; j < SUDOKU; j++) { row = j % BLOCK + d; col = j / BLOCK + (i / BLOCK) * BLOCK; c_block[i][j] = board[col][row]; c_block[SUDOKU + i][j] = board[i][j]; c_block[SUDOKU * 2 + i][j] = board[j][i]; } d = (d + BLOCK) % SUDOKU; } return isRight(c_block); } private boolean isRight(char[][] chars){ int row = chars[0].length; Set<Character> set = new HashSet<Character>(); for (char[] aChar : chars) { int count = 0; for (int j = 0; j < row; j++) { if (aChar[j] == '.') { count++; continue; } set.add(aChar[j]); } if (set.size() + count < 9) { return false; } set.clear(); } return true; }};
阅读全文
0 0
- 判定一个数独是否有效。
- Valid Sudoku 检查数独是否有效 @LeetCode
- valid-sudoku判断是否是有效数独
- LeetCode:36. Valid Sudoku,数独是否有效 :
- 如何判定一个数是否为奇数?
- 判定一个数是否是素数
- 有效数独 Valid Sudoku
- 如何快速判定一个数是否2的幂数
- 有效数判定 Valid Number
- 随机产生一个数并判定是否为素数
- 判断数独是否正确
- Valid Sudoku 有效的数独
- leetcode 36. Valid Sudoku 数独有效
- 判定时间是否有效的函数
- 判定字符串形式IP是否有效
- 判定一个组数是否是一个搜索二叉数的后序遍历
- 数独的一个解法
- 判定一个大数是否素数
- js判断全选与全不选以及,js全不选时全选按钮会被勾掉
- NYOJ 23 取石子巴什博奕
- Python3安装geohash
- 【大数据部落】用R进行网站评论文本挖掘聚类
- cocos2dx获取系统当前时间
- 判定一个数独是否有效。
- Linux的吉祥物企鹅的名字?
- 【大数据部落】基于决策树的银行信贷风险预警模型
- 微信小程序推荐大全之201~300个
- git add详解
- undefined symbol: node_module_register 错误原因
- POJ 1067 取石子 Wythoff 博弈
- 【大数据部落】WEKA文本挖掘分析垃圾邮件分类模型
- java集合框架之List实现类的性能分析