说明

算法:Valid Sudoku
LeetCode地址:https://leetcode.com/problems/valid-sudoku/

题目:
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

  1. Each row must contain the digits 1-9 without repetition.
  2. Each column must contain the digits 1-9 without repetition.
  3. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.

    A partially filled sudoku which is valid.

The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’.

Example 1:

Input:
[["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]
]
Output: true

Example 2:

Input:
[["8","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]
]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.

Note:

A Sudoku board (partially filled) could be valid but is not necessarily solvable.
Only the filled cells need to be validated according to the mentioned rules.
The given board contain only digits 1-9 and the character ‘.’.
The given board size is always 9x9.

解题思路

数独问题是个二维的图,实际是有三个条件下都没有重复的数据,
很明显用Set来过滤,如果数据加入失败,那么表示数据已经存在。
接下来降维解决,把二维图化解为三个条件的一维处理。

  1. row横向方向
  2. column纵向方向
  3. cube方框内是否重复,横向/3 - 纵向/3 , 实际就是cube的唯一标识,
    比如index[0, 1, 2]的横向纵向都是0-0.

时间复杂度 O(9*9)

代码实现

import java.util.HashSet;
import java.util.Set;public class ValidSudoku {public boolean isValidSudoku(char[][] board) {Set<String> visitSet = new HashSet<>(81);for (int row = 0; row < 9; row++) {for (int column = 0; column < 9; column++) {char data = board[row][column];if (data == '.') {continue;}if (!visitSet.add(data + " row " + row)) {System.out.println("duplicate board[" + row + "][" + column + "]" + " ;data: " + data+ " row " + row);return false;}if (!visitSet.add(data + " column " + column)) {System.out.println("duplicate board[" + row + "][" + column + "]" + " ;data: " + data+ " column " + column);return false;}if (!visitSet.add(data + " cube " + row/3 + '-' + column/3) ) {System.out.println("duplicate board[" + row + "][" + column + "]" + " ;data: " + data+ " cube " + row/3 + '-' + column/3);return false;}}}return true;}public static void main(String[] args) {char[][] board1 = {{'5','3','.','.','7','.','.','.','.'},{'6','.','.','1','9','5','.','.','.'},{'.','9','8','.','.','.','.','6','.'},{'8','.','.','.','6','.','.','.','3'},{'4','.','.','8','.','3','.','.','1'},{'7','.','.','.','2','.','.','.','6'},{'.','6','.','.','.','.','2','8','.'},{'.','.','.','4','1','9','.','.','5'},{'.','.','.','.','8','.','.','7','9'}};char[][] board2 = {{'8','3','.','.','7','.','.','.','.'},{'6','.','.','1','9','5','.','.','.'},{'.','9','8','.','.','.','.','6','.'},{'8','.','.','.','6','.','.','.','3'},{'4','.','.','8','.','3','.','.','1'},{'7','.','.','.','2','.','.','.','6'},{'.','6','.','.','.','.','2','8','.'},{'.','.','.','4','1','9','.','.','5'},{'.','.','.','.','8','.','.','7','9'}};ValidSudoku obj = new ValidSudoku();System.out.println("Output 1: " + obj.isValidSudoku(board1));System.out.println("Output 2: " + obj.isValidSudoku(board2));}
}

运行结果

Output 1: true
duplicate board[2][2] ;data: 8 cube 0-0
Output 2: false

代码执行效率

Runtime: 15 ms, faster than 65.36% of Java online submissions for Valid Sudoku.
Memory Usage: 46.9 MB, less than 5.15% of Java online submissions for Valid Sudoku.

总结

二维图问题降维为三个条件的非重复数据解决。

代码下载:
https://github.com/zgpeace/awesome-java-leetcode/blob/master/code/LeetCode/src/popular/ValidSudoku.java

算法:Valid Sudoku(有效的数独)相关推荐

  1. leetcode之Valid Sudoku有效的数独(一步步改进代码)

    题目链接:Valid Sudoku有效的数独 题目已经十分确定的说了只有1~9,因此标记法无疑是非常好的选择. 基本思路:对行.列.小数独块分别用一个size为9的数组来标记数字1~9在本行(列/块) ...

  2. LeetCode 36. Valid Sudoku(九宫格数独)

    依次检查每行,每列,每个子九宫格是否出现重复元素,如果出现返回false,否则返回true. 难点在于表示第i个九宫格每个格点的坐标. 观察行号规律: 第0个九宫格:000111222; 第1个九宫格 ...

  3. LeetCode 36 Valid Sudoku(有效数独)(*)

    翻译 数独板被部分填充,空格部分用'.'来填充.一个部分填充的数组是否有效只需要看其填充的部分即可. 原文 代码 这道题写了一会,错了--因为输入太懒搞了,就直接看了别人写的-- class Solu ...

  4. Leetcode36.Valid Sudoku有效的数独

    判断一个 9x9 的数独是否有效.只需要根据以下规则,验证已经填入的数字是否有效即可. 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1-9 在每一个以粗实线分隔的 ...

  5. 36. Valid Sudoku数独判断

    题目:数独填写正确判断 https://leetcode.com/problems/valid-sudoku/description/ Determine if a Sudoku is valid, ...

  6. leetcode 36. Valid Sudoku | 37. Sudoku Solver(数独)

    36. Valid Sudoku https://leetcode.com/problems/valid-sudoku/ 题解 class Solution {public boolean isVal ...

  7. 【LeetCode从零单排】No36 Valid Sudoku

    题目       判断数独是否成立的一道题,看的是某大神的答案,写的太漂亮了. Determine if a Sudoku is valid, according to: Sudoku Puzzles ...

  8. 【LeetCode】36. Valid Sudoku

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

  9. [leetcode]36. Valid Sudoku c语言

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

  10. Valid Sudoku leetcode java

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

最新文章

  1. xml放在工程的那个路径下_Ubuntu下配置pyrouge
  2. YSLOW法则中,为什么yahoo推荐用GET代替POST?
  3. 深度学习必须掌握的 13 种概率分布
  4. OpenStack 虚拟机热迁移流程图
  5. 从0系统学 Android--1.1认识 Android
  6. 浙江省二级计算机vfp,浙江省计算机2级vfp程序调试真题集.doc
  7. Python-web框架 fastapi
  8. Java实战项目,附带源码+视频教程,收藏!
  9. 什么是图像上的频率?
  10. css3学习以及移动端开发基本概念的思考
  11. ECharts基础学习 (第二天)
  12. OpenMP学习笔记之常用指令parallel/sections/critical 其余待续
  13. mybatisplus报 Invalid bound statement (not found):
  14. Python3 浮点数精度问题
  15. Android 控件 RecyclerView
  16. 从零开始学WEB前端——JavaScript流程控制语句
  17. 僵尸毁灭工程服务器耐久修改,僵尸毁灭工程无限负重的修改方法
  18. ABAP 开发系列(08): SAP Open SQL
  19. 《Python之禅》
  20. sqlserver战德臣_慕课战德臣数据库系统讲义PPT.zip

热门文章

  1. 【转】nodejs 压缩文件 zip-local
  2. vue-cli3 处理静态文件 下载引用
  3. [转]Vue Cli3 + VS Code 愉快调试
  4. C#(int)中Convert、Parse、TryParse的区别
  5. 比较JavaScript中的集合及其检索效率
  6. hdoj1043 Eight(逆向BFS+打表+康拓展开)
  7. CentOS 7.4 安装Nginx
  8. Zookeeper 集群的安装与部署
  9. VMWaer克隆centos后网络的问题解决
  10. 根据当前节点获取所有上层结构的组织(递归算法)