关闭

LeetCode---Valid Sudoku解题分析

1247人阅读 评论(2) 收藏 举报
分类:

题意描述:判断矩阵是否是一个数独矩阵。所谓的数独矩阵就是9*9的矩阵,每一行一个元素只出现一次、每一列一个元素只出现一次,并且在任意一个小矩阵中一个元素只出现一次(所说的元素指0-9这九个数字)

矩阵如下图所示:


解题分析:解题思路:注意题目中说的是只要当前已经填充的数字是合法的就可以,不一定要这个数独是有解的。依次判断已给定的矩阵每一行、每一列、以及每个小矩阵上是否是有重复数字即可;

public boolean isValid(boolean[] visited, char ch){//判断当前位置是否为'.',或是否被访问过
	if(ch == '.')//如果当前位置为'.'则返回true
		return true;
	
	int num = ch - '0';
	if(num < 1 || num > 9 || visited[num-1])//如果当前位置访问过则返回false
		return false;
	
	visited[num-1] = true;//将标记数字标记为true,表示该数字已访问过
	return true;
}
public boolean isValidSudoku(char[][] board){		
	boolean[] visited = new boolean[9];
	//判断每一行是否有重复数字
	for(int i=0; i<9; i++){
		Arrays.fill(visited, false);
		for(int j=0; j<9; j++){
			if(!isValid(visited, board[i][j]))
				return false;
		}
	}
	//判断每一列是否有重复数字
	for(int i=0; i<9; i++){
		Arrays.fill(visited, false);
		for(int j=0; j<9; j++){
			if(!isValid(visited, board[j][i]))
				return false;
		}
	}
	//判断每个子矩阵是否有重复数字
	for(int i=0; i<9; i+=3){
		for(int j=0; j<9; j+=3){
			Arrays.fill(visited, false);
			for(int k=0; k<9; k++){
				if(!isValid(visited, board[i+k/3][j+k%3]))
					return false;
			}
		}
	}
	
	return true;
}



0
0
查看评论

LeetCode(36)Valid Sudoku

题目Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled wi...
  • fly_yr
  • fly_yr
  • 2015-08-25 20:40
  • 2247

Valid Sudoku -- LeetCode

原题链接: http://oj.leetcode.com/problems/valid-sudoku/  这道题是Sudoku Solver的一个子问题,在解数独的时候我们需要验证当前数盘是否合法。其实思路比较简单,也就是用brute force。对于每一行,每一列,每个九宫格...
  • linhuanmars
  • linhuanmars
  • 2014-03-10 03:52
  • 13184

LeetCode: 36. Valid Sudoku

LeetCode: 36. Valid Sudoku
  • yanglingwell
  • yanglingwell
  • 2017-03-27 08:13
  • 1409

[leetcode]Valid Sudoku(判断有效数独 C语言实现)

Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells a...
  • sanmao0816
  • sanmao0816
  • 2015-03-30 21:55
  • 1335

leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles -...
  • wangyaninglm
  • wangyaninglm
  • 2016-05-04 22:07
  • 1111

LeetCode:Valid Sudoku

Valid Sudoku Total Accepted: 60134 Total Submissions: 206702 Difficulty: Easy Determine if a Sudoku is valid,...
  • itismelzp
  • itismelzp
  • 2016-01-11 10:44
  • 349

Valid Sudoku 检查数独是否有效 @LeetCode

这类问题关键还是要把结构写清楚,要让code self-explanatory! package Level2; /** * Valid Sudoku * etermine if a Sudoku is valid, according to: Sudoku Puzzles - The ...
  • hellobinfeng
  • hellobinfeng
  • 2013-11-06 00:48
  • 5148

[LeetCode]36.Valid Sudoku

【题目】 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells a...
  • SunnyYoona
  • SunnyYoona
  • 2014-01-20 13:42
  • 1493

Valid Sudoku (Java)

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are fill...
  • tofu_jelly
  • tofu_jelly
  • 2014-10-28 15:34
  • 861

Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are fi...
  • dutsoft
  • dutsoft
  • 2014-08-16 12:00
  • 301
    个人资料
    • 访问:240142次
    • 积分:4434
    • 等级:
    • 排名:第8034名
    • 原创:209篇
    • 转载:25篇
    • 译文:0篇
    • 评论:16条
    代码库
    博客专栏
    最新评论