本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章

  1. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  2. [LeetCode] Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. [LeetCode] Valid Sudoku 验证数独

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  4. LeetCode 36 Valid Sudoku

    Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...

  5. 【leetcode】Valid Sudoku

    题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...

  6. ACM : POJ 2676 SudoKu DFS - 数独

    SudoKu Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu POJ 2676 Descr ...

  7. ACM: ICPC/CCPC Sudoku DFS - 数独

    Sudoku Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/65535K (Java/Other) Total Submis ...

  8. Leetcode: Sudoku Solver

    July 19, 2015 Problem statement: Write a program to solve a Sudoku puzzle by filling the empty cells ...

  9. Leetcode Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

随机推荐

  1. Node.js HTTPS

    稳定性: 3 - 稳定 HTTPS 是基于 TLS/SSL 的 HTTP 协议.在 Node 里作为单独的模块来实现. 类: https.Server 这是 tls.Server 的子类,并且和 ht ...

  2. AbstractQueuedSynchronizer源码解读

    1. 背景 AQS(java.util.concurrent.locks.AbstractQueuedSynchronizer)是Doug Lea大师创作的用来构建锁或者其他同步组件(信号量.事件等) ...

  3. springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)

    HandlerMethodReturnValueHandler是用于对Controller中函数执行的返回值进行处理操作的,springMVC提供了多个HandlerMethodReturnValue ...

  4. Android源码解析——AsyncTask

    简介 AsyncTask 在Android API 3引入,是为了使UI线程能被正确和容易地使用.它允许你在后台进行一些操作,并且把结果带到UI线程中,而不用自己去操纵Thread或Handler.它 ...

  5. MFC误报内存泄露的修复

    在debug状态退出程序的时候,VS会在输出窗口列出可能的内存泄露的地方. MFC中使用DEBUG_NEW能够更方便的定位泄露的地点.但假如MFC的dll释放""过早"& ...

  6. springMVC源码分析--SimpleUrlHandlerMapping(四)

    上一篇博客springMVC源码分析--AbstractUrlHandlerMapping(三)中我们介绍了AbstractUrlHandlerMapping,主要介绍了一个handlerMap的ur ...

  7. 信用卡3D验证相关资料

    3D 验证服务,是银行与VISA .MASTERCARD国际组织联合推出的为保障银行维萨及万事达信用卡持卡客户网上交易安全,防范网上伪冒交易的一项信用卡网上支付安全验证服务( 维萨卡使用的验证服务叫& ...

  8. Android Studio安装Genymotion插件

    Android Studio安装Genymotion插件 Eclipse就不介绍了,谷歌都已经放弃Eclipse了,你还在坚持什么. 安装Genymotion 官网:https://www.genym ...

  9. JavaScript与jQuery获取相邻控件

    原始代码如下,需求是onclick中的OpenIframe方法捕捉到input中的value值,由于某些限制无法使用正常的操作dom根据name值来取,所以决定通过相邻空间的方式获取 <div& ...

  10. Android初级教程:ViewPage使用详解

    转载本博客,请注明出处:http://blog.csdn.net/qq_32059827点击打开链接 ViewPage使用之一就是轮播广告,就以此为出发点,来详细解析一下ViewPage的使用和加载机 ...