`
hcx2013
  • 浏览: 83308 次
社区版块
存档分类
最新评论

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 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.

 

public class Solution {
    public boolean isValidSudoku(char[][] board) {
        Set<Character> set = new HashSet<Character>();
        for (int i = 0; i < board.length; i++) {
        	set.clear();
        	for (int j = 0; j < board.length; j++) {
        		if (board[i][j]!='.' && !set.add(board[i][j])) {
        			return false;
        		}
        	}
        }
        for (int i = 0; i < board.length; i++) {
        	set.clear();
        	for (int j = 0; j < board.length; j++) {
        		if (board[j][i]!='.' && !set.add(board[j][i])) {
        			return false;
        		}
        	}
        }
        for (int i = 0; i < board.length/3; i++) {
        	for (int j = 0; j < board.length/3; j++) {
        		set.clear();
        		for (int a = i*3; a < i*3+3; a++) {
        			for (int b = j*3; b < j*3+3; b++) {
        				if (board[a][b]!='.' && !set.add(board[a][b])) {
                			return false;
                		}
        			}
        		}
        	}
        }
        return true;
    }
}

 

0
4
分享到:
评论

相关推荐

    Sudoku solver in C++ (数独计算器)

    A Sudoku solver implemented in C++. It can solve a given Sudoku problem, or count the possibilities for all valid Sudoku grids.

    cpp-算法精粹

    Valid Sudoku Trapping Rain Water Rotate Image Plus One Climbing Stairs Set Matrix Zeroes Gas Station Candy Majority Element Rotate Array Contains Duplicate Contains Duplicate II Contains Duplicate III...

    dna匹配leetcode-leetcode:leetcode刷题

    Valid Sudoku 数组 遍历 Sudoku Solver 深度优先遍历 回溯 先检查后修改 Group Anagrams 排序 unordered_map Minimum Window Substring 两个指针遍历 map Maximal Rectangle 栈 局部递增 或者 动态规划 Binary Tree ...

    LeetCode最全代码

    ...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...

    lrucacheleetcode-LeetCode:这个库用于总结leetcode中遇到的习题,期望按照数据结构和常用方法分成2类,进行总结,

    lru cache leetcode LeetCode 这个库用于总结leetcode中遇到的习题 常用数据结构习题总结 1.线性表 解决进度 No. Describition mark 1 Remove Duplicates from ...Valid Sudoku 15 Trapping Rain W

    leetcode卡-leetcode:利特码解决方案

    Valid Sudoku linked list Palindrome linked list Linked List Cycle trees Convert Sorted Array to Binary Search Tree string and search First Bad Version Dynamic Programing *** Climbing Stairs Set Matrix...

    lovely-nuts:这是一个可爱的坚果

    Practice-Leetcode 这是一个Chinese School Girl:China:用来练习leetcode的文档.每道下面的题都有详细的解题思路,和知识点分析,尽请参考。...36.Valid Sudoku set去重复 2018/04/19: 038.Count and Say 递归 040.C

    leetcode添加元素使和等于-leetcode_py:leetcode的python版本问题

    leetcode添加元素使和等于 leetcode_py Python version of leetcode problems 33 Search in Rotated Sorted Array 问题:找到经过旋转的有序数组中是否有目标的数。 解法:基于二分的方法,根据 ...Valid Sudoku 问题

    sudoku_validator:Prime 订阅者的练习

    示例用法: $ sudoku-validator ./valid_complete.sudoku This sudoku is valid.$ sudoku-validator ./valid_incomplete.sudoku This sudoku is valid, but incomplete.$ sudoku-validator ./invalid_complete.sudo

    lintcode:LintCode题解,LintCode是LeetCode.com网站的增强版,题目更优质,覆盖知识点更多

    #LintCode题解##PDF下载本书即将由电子工业出版社出版,因此这个Repo更新会略落后于纸质版。欢迎大家购买纸质版,支持我持续更新本书。C++ 文件夹下是C++版,内容一摸一样,代码是用C++写的,Java 文件夹下是Java版...

    sudoku-checker

    数独检查器 检查您的数独是否有效 要求: Java 8 Maven3 跑步: mvn package java -jar target/sudoku-checker-1.0-SNAPSHOT.jar src/test/resources/valid.txt 报告: mvn site

    leetcode2-Codonfest:鳕鱼{ON}节2020

    leetcode 2 ...以及在竞争性编程中组织了第一个 1.5 个月的指导计划。 该计划将包括 2 个阶段。...学员将与导师配对,在为期 ...个月的时间里,学员将获得学员每天需要尝试的不同难度级别的学习材料和问题。...

    数独解算器

    数独解算器 该项目的主要思想是构建Sudoku难题求解器。 解算器必须遵守所有标准的数独难题规则。 数独规则: Sudoku puzzle is a 9-by-9 grid of squares, consisting of 9 ...2. is_valid 3. solve_sudoku Function

    Sudoku:生成具有不同级别的可解决的数独和回溯,正向搜索解决方案

    is_valid :如果给定的移动(空插槽中的数字)有效,则返回布尔值 generate.py 产生generate :在给定的难度级别上生成数独谜题,并返回预期的答案。 难度级别:“易”,“中”,“难”和“ v_hard ”,其中“易”...

    Lee-College-Python-Class2

    Lee-College-Python-Class2 ... 一些命令用于标识下一个空的拼图插槽,例如def find_next_empty,其他命令def is_valid将帮助玩家发现所选插槽是否有效。 def solv_sudoku是回溯难题以解决难题的命令。

Global site tag (gtag.js) - Google Analytics