题目

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.

题解

这道题利用的是HashSet的唯一性来帮助check。

先按每行check,如果是'.'说明还没填字,是合法的,往下走,如果没在set中存过就加一下,如果便利过程中出现了在set中存在的key值,说明有重复的数字在一行,不合法,return false。

再按照这个方法check列。

最后按照这个方法check小方块。

注意小方块的ij取法。对于当前这块板子来说,总共有9个小方格,按0~8从左到右依次编号。

按编号求'/'就是求得当前小方格的第一行横坐标,因为每个小方格有3行,所以循环3次。

按编号求'%'就是求得当前小方格的第一列纵坐标,因为每个小方格有3列,所以循环3次。

对9个小方格依次走一边,就完成了检查小方格的工作。

代码如下:

 1 public boolean isValidSudoku(char[][] board) {
 2     HashSet<Character> set = new HashSet<Character>();
 3     // Check for each row
 4     for (int i = 0; i < 9; i++) {
 5         for (int j = 0; j < 9; j++) {
 6             if (board[i][j] == '.')
 7                 continue;
 8             if (set.contains(board[i][j]))
 9                 return false;
             set.add(board[i][j]);
         }
         set.clear();
     }
 
     // Check for each column
     for (int j = 0; j < 9; j++) {
         for (int i = 0; i < 9; i++) {
             if (board[i][j] == '.')
                 continue;
             if (set.contains(board[i][j]))
                 return false;
             set.add(board[i][j]);
         }
         set.clear();
     }
 
     // Check for each sub-grid
     for (int k = 0; k < 9; k++) {
         for (int i = k/3*3; i < k/3*3+3; i++) {
             for (int j = (k%3)*3; j < (k%3)*3+3; j++) {
                 if (board[i][j] == '.')
                     continue;
                 if (set.contains(board[i][j]))
                     return false;
                 set.add(board[i][j]);
             }
         }
         set.clear();
     }
     
     return true;
 }

Valid Sudoku leetcode java的更多相关文章

  1. Valid Sudoku leetcode

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

  2. Valid Palindrome leetcode java

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  3. Longest Valid Parentheses leetcode java

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  4. Valid Parentheses leetcode java

    题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  5. Valid Number leetcode java

    题目: Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...

  6. Java [leetcode 36]Valid Sudoku

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

  7. LeetCode——Valid Sudoku

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

  8. [LeetCode] 036. Valid Sudoku (Easy) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...

  9. leetcode@ [36/37] Valid Sudoku / Sudoku Solver

    https://leetcode.com/problems/valid-sudoku/ Determine if a Sudoku is valid, according to: Sudoku Puz ...

随机推荐

  1. 【BZOJ 1430】 1430: 小猴打架 (Prufer数列)

    1430: 小猴打架 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 625  Solved: 452 Description 一开始森林里面有N只互不相 ...

  2. luoguP3175 [HAOI2015]按位或 min-max容斥 + 高维前缀和

    考虑min-max容斥 \(E[max(S)] = \sum \limits_{T \subset S} min(T)\) \(min(T)\)是可以被表示出来 即所有与\(T\)有交集的数的概率的和 ...

  3. IEEEXtreme Practice Community Xtreme9.0 - Dictionary Strings

    Dictionary Strings 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/dictio ...

  4. 自动添加 Qt 开发生成的 exe 所需的依赖环境

    双击获取 exe 文件路径 cd 进入文件目录的命令 调用 Qt 自带的软件进行环境配置,命令如下 windeployqt ***.exe 自动配置了依赖环境

  5. 1、QThreadPool线程池的使用,线程和Widget通过QMetaObject::invokeMethod交互。

    自定义一个QThreadPool,N个线程QRunnable,线程和Widget通过QMetaObject::invokeMethod交互. QRunnable非继承自QObject,所以不可以用信号 ...

  6. 使用httpclient需要的maven依赖

    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --> <dependency& ...

  7. spring cloud 学习(11) - 用fastson替换jackson及用gb2312码输出

    前几天遇到一个需求,因为要兼容旧项目的编码格式,需要spring-cloud的rest接口,输出gb2312编码,本以为是一个很容易的事情,比如下面这样: @RequestMapping(method ...

  8. 在windows server 2008 R2 64bit上面配置PI OPC Server的DCOM

    今天想配置PI OPC SERVER的DCOM设置,但是发现在“运行dcomcnfg->组件服务-计算机-我的电脑-DCOM设置”中找不到PI OSI DA Server.如下图所示 这是以前从 ...

  9. oracle 两个逗号分割的字符串 如何判断是否其中有相同值

    比如字段A: 'ab,cd,ef,gh'字段B: 'aa,bb,cc,dd' 没有相同值 字段A: 'ab,cd,ef,gh'字段B: 'aa,bb,cd,dd' 有相同值cd 1.CREATE OR ...

  10. C++ STL set::find的用法

      参考: http://blog.csdn.net/lihao21/article/details/6302196 /* class for function predicate * - opera ...