- 浏览: 171724 次
- 性别:
- 来自: 济南
文章分类
最新评论
Determine if a Sudoku is valid. 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.
检测一个数独是否有效,数独的规则大家不明白的可以去百度一下。这里我们只需要检查当前的状态是否有效,而不用找到一个解。所以我们只要检查每行,每列以及没九个小方格内是否都没有重复数字出现就可以了。代码如下:
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.
检测一个数独是否有效,数独的规则大家不明白的可以去百度一下。这里我们只需要检查当前的状态是否有效,而不用找到一个解。所以我们只要检查每行,每列以及没九个小方格内是否都没有重复数字出现就可以了。代码如下:
public class Solution { public boolean isValidSudoku(char[][] board) { HashSet<Character> set = new HashSet<Character>(); for(int i = 0; i < board.length; i++) { for(int j = 0; j < board[0].length; j++) { if(set.contains(board[i][j])) return false; else if(board[i][j] != '.') set.add(board[i][j]); } set.clear(); } for(int i = 0; i < board[0].length; i++) { for(int j = 0; j < board.length; j++) { if(set.contains(board[j][i])) return false; else if(board[j][i] != '.') set.add(board[j][i]); } set.clear(); } for(int m = 0; m < 3; m++) { for(int n = 0; n < 3; n++) { for(int i = 3 * m; i < 3 * m + 3; i++) { for(int j = 3 * n; j < 3 * n + 3; j++) { if(set.contains(board[i][j])) return false; else if(board[i][j] != '.') set.add(board[i][j]); } } set.clear(); } } return true; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 221Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 219You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 336Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 325Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 443Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 498Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 423Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 611Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 420The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 378Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 511Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 521Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 364All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 853Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 876Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 547Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 592Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 757Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 724You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 623For a undirected graph with tre ...
相关推荐
本游戏界面简洁,并有如下功能: 1. 按照5级难度自动布局或手工布局,并具有计时功能 2. 具有“下一步”提示,和步骤追踪“回退”功能 3. 能够保存布局和求解进度 4. 可电脑求解“唯一解”和“所有解” ...
Sudoku数独游戏
Sudoku数独程序编程.pdf
Java SuDoKu数独游戏,一种运用纸、笔进行演算的逻辑游戏。用Java写出的j2me版本的数独,运行于基于Java的手机客户端中。
“数独”(Su Doku)是你和方格之间意志的较量,你不需要大部头词典的帮助,也无需去书店查阅参考书籍,你所需要的东西已经全部在你的大脑中了。在这个精彩旅程中,只有你和“数独”。 这款小游戏或称之为...
写的一个数独游戏,自我感觉算法还可以,就放上来了
本游戏界面简洁,功能完善,堪称数独游戏的完美版本: 1. 按照5级难度自动布局或手工布局,并具有计时功能 2. 具有“下一步”提示,和步骤追踪“回退”功能 3. 能够保存布局和求解进度 4. 可电脑求解“唯一解”和...
数独,九宫格种运用纸、笔进行演算的逻辑游戏。玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行、每一列、每一个粗线宫内的数字均含1-9,不重复。
是一个用vc++实现的数独游戏,可以锻炼你的智力哦!
A Sudoku solver implemented in C++. It can solve a given Sudoku problem, or count the possibilities for all valid Sudoku grids.
数独问题算法,针对任意数独问题的一般算法
该程序是9*9数独的解决工具,可以根据用户的输入,然后一键补充完整,从而解决数独问题。
javascript sudoku 数独智力游戏生成代码,喜欢的朋友可以参考下。
这是一篇关于数独游戏算法的文章,希望对喜爱数独游戏的编程朋友有所帮助。英文版 This is an essay of The Mathematics of Sudoku. Hoping this paper is useful for game programmer.
用Prolog 解数独, constraint satisfaction problem的方法
数独sudoku:整个矩阵由为9*9的矩阵,矩阵中又分成9个3*3的小矩阵,在每个方格中填入1-9 的数字,必须符合列中无重复数字,行中无重复数字,小矩阵中无重复数字 本游戏介绍: 1、游戏模式 a、练习模式,无计时,...
数独验证器
1、手工输入不完整的数独,可以使用"上下左右"按键移动输入框焦点,输入框只允许输入"1-9"的个位数字,其他字符不允许输入。 2、单击"求解"按钮进行数独运算,程序会检查您输入的数独合法性,不符合规则的数独将会...
数独游戏,简易版,提供五种难度要求自动出题解题,有人性化操作界面。