Leetcode_36_Valid Sudoku
本文是在学习中的总结,欢迎转载但请注明出处: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;
- }
Leetcode_36_Valid Sudoku的更多相关文章
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- [LeetCode] Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- [LeetCode] Valid Sudoku 验证数独
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- LeetCode 36 Valid Sudoku
Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...
- 【leetcode】Valid Sudoku
题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...
- ACM : POJ 2676 SudoKu DFS - 数独
SudoKu Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu POJ 2676 Descr ...
- ACM: ICPC/CCPC Sudoku DFS - 数独
Sudoku Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/65535K (Java/Other) Total Submis ...
- Leetcode: Sudoku Solver
July 19, 2015 Problem statement: Write a program to solve a Sudoku puzzle by filling the empty cells ...
- Leetcode Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
随机推荐
- 02_版本控制工具SVN
SubVersion: 安装:根据电脑版本选择安装64或32位的subversion,尽量不要选择中文或者有空格的目录安装 版本控制仓库: 创建命令:SVNadmin create 目录 启动SVN服 ...
- python通过token登录,并爬取数据实例
from bs4 import BeautifulSoup import requests class Zabbix(object): def __init__(self, headers): sel ...
- MongoDB 高级索引
考虑以下文档集合(users ): { "address": { "city": "Los Angeles", "state&qu ...
- PHP 5 Math 函数
PHP Math 简介 Math 函数能处理 integer 和 float 范围内的值. 安装 PHP Math 函数是 PHP 核心的组成部分.无需安装即可使用这些函数. PHP 5 Math 函 ...
- Lucene总结
数据的分类 结构化数据:有固定类型或者有固定长度的数据 例如:数据库中的数据(mysql,oracle等), 元数据(就是windows中的数据) 结构化数据搜索方法: 数据库中数据通过sql语句可以 ...
- Activtiy完全解析(二、layout的inflate过程)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/52457893 本文出自:[openXu的博客] 在上一篇文章<Activtiy完全 ...
- TextView + Spanned实现图文混排以及图片点击交互
最近要实现图文混排的需求,webview过大,所以想到了用SpannableStringBuilder来实现. 不过参考了大量国内文章,大多数是教你如何实现图文混排,并没有提及图片点击交互的.有翻阅了 ...
- activiti实战系列 排他网关(ExclusiveGateWay)
流程图 12.2:部署流程定义+启动流程实例 12.3:查询我的个人任务 12.4:完成我的个人任务 说明: 1) 一个排他网关对应一个以上的顺序流 2) 由排他网关流出的顺序流都有个 ...
- Linux命令—压缩及其他
(1)为了更好的传送和保存文件,需要对某些文件和目录进行压缩和解压缩操作,Linux 提供了强大的压缩.解压缩命令,常用的tar命令. (2)在Linux中,如果要使用储存设备(硬盘.光驱.移动 ...
- JAVA之旅(三十四)——自定义服务端,URLConnection,正则表达式特点,匹配,切割,替换,获取,网页爬虫
JAVA之旅(三十四)--自定义服务端,URLConnection,正则表达式特点,匹配,切割,替换,获取,网页爬虫 我们接着来说网络编程,TCP 一.自定义服务端 我们直接写一个服务端,让本机去连接 ...