Leetcode_36_Valid Sudoku
来源:互联网 发布:ubuntu如何创建文件夹 编辑:程序博客网 时间:2024/05/21 00:55
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42528601
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.
思路:
(1)题意为判断一个数独是否有效。
(2)题意不是让我们求解出整个数独(当然,如果真的求解还是很复杂的),而是判断数独中已有数据是否有效。
(3)本文的思路还是暴力破解,因为没有想到其它好的方法。对数独对应的9*9的二维数组进行遍历,对于任意一个不为'.'的数字,都需要对其所在的行、列、以及3*3的块区域进行判断,如果有重复出现的情况,那么数独就是无效的,具体见下方代码。
(4)希望本文对你有所帮助。
算法代码实现如下:
public boolean isValidSudoku(char[][] board) {for (int i = 0; i < board.length; i++) {for (int j = 0; j < board[i].length; j++) {char curr = board[i][j];if(curr=='.'){continue;}//行for (int k = j+1; k < board.length; k++) {if(curr==board[i][k]){return false;}}//列for (int k = i+1; k < board.length; k++) {if(curr==board[k][j]){return false;}}//3*3 方块if(i>=0&&i<3&&j>=0&&j<3){int count=0;for (int k1 = 0; k1 < 3; k1++) {for (int k2 = 0; k2 < 3; k2++) {if(board[k1][k2]==curr){count++;}if(count>1) return false;}}}if(i>=0&&i<3&&j>=3&&j<6){int count=0;for (int k1 = 0; k1 < 3; k1++) {for (int k2 = 3; k2 < 6; k2++) {if(board[k1][k2]==curr){count++;}if(count>1) return false;}}}if(i>=0&&i<3&&j>=6&&j<9){int count=0;for (int k1 = 0; k1 < 3; k1++) {for (int k2 = 6; k2 < 9; k2++) {if(board[k1][k2]==curr){count++;}if(count>1) return false;}}}if(i>=3&&i<6&&j>=0&&j<3){int count=0;for (int k1 = 3; k1 < 6; k1++) {for (int k2 = 0; k2 < 3; k2++) {if(board[k1][k2]==curr){count++;}if(count>1) return false;}}}if(i>=3&&i<6&&j>=3&&j<6){int count=0;for (int k1 = 3; k1 < 6; k1++) {for (int k2 = 3; k2 < 6; k2++) {if(board[k1][k2]==curr){count++;}if(count>1) return false;}}}if(i>=3&&i<6&&j>=6&&j<9){int count=0;for (int k1 = 3; k1 < 6; k1++) {for (int k2 = 6; k2 < 9; k2++) {if(board[k1][k2]==curr){count++;}if(count>1) return false;}}}if(i>=6&&i<9&&j>=0&&j<3){int count=0;for (int k1 = 6; k1 < 9; k1++) {for (int k2 = 0; k2 < 3; k2++) {if(board[k1][k2]==curr){count++;}if(count>1) return false;}}}if(i>=6&&i<9&&j>=3&&j<6){int count=0;for (int k1 = 6; k1 < 9; k1++) {for (int k2 = 3; k2 < 6; k2++) {if(board[k1][k2]==curr){count++;}if(count>1) return false;}}}if(i>=6&&i<9&&j>=6&&j<9){int count=0;for (int k1 = 6; k1 < 9; k1++) {for (int k2 = 6; k2 < 9; k2++) {if(board[k1][k2]==curr){count++;}if(count>1) return false;}}}}}return true;}
3 0
- Leetcode_36_Valid Sudoku
- Sudoku
- sudoku
- Sudoku
- sudoku
- Sudoku
- Sudoku
- sudoku
- sudoku
- Sudoku
- Sudoku
- Sudoku
- Sudoku
- Sudoku
- Sudoku
- Sudoku
- Sudoku
- Sudoku
- 高校云平台(一):评教活动引发思考
- uva 10717
- 关于在spring中配置quatrz任务执行两次的问题
- 常用域名后缀的含义
- uva10717(数论)
- Leetcode_36_Valid Sudoku
- Static成员变量,static成员函数解析
- IOS中UIScrollView的属性和委托方法
- nyoj 975 关于521
- 比较字母大小 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 任意给出两个英文字母,比较它们的大小,规定26个英文字母A,B,C.....Z依次从大到小。
- awk中处理asc码
- 移动端Web开发调试之Chrome远程调试(Remote Debugging)
- 第二十周总结-Aha!有趣的C++资料
- MAC和PHY关系