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

解题思路:

传说中的数独(九宫格)问题,老实遍历三个规则即可:

JAVA实现:

  1. static public boolean isValidSudoku(char[][] board) {
  2. for (int i = 0; i < board.length; i++) {
  3. HashMap<Character, Integer> hashmap = new HashMap<Character, Integer>();
  4. for (int j = 0; j < board[0].length; j++) {
  5. if (board[i][j] != '.') {
  6. if (hashmap.containsKey(board[i][j]))
  7. return false;
  8. hashmap.put(board[i][j], 1);
  9. }
  10. }
  11. }
  12. for (int j = 0; j < board[0].length; j++) {
  13. HashMap<Character, Integer> hashmap = new HashMap<Character, Integer>();
  14. for (int i = 0; i < board.length; i++) {
  15. if (board[i][j] != '.') {
  16. if (hashmap.containsKey(board[i][j]))
  17. return false;
  18. hashmap.put(board[i][j], 1);
  19. }
  20. }
  21. }
  22. for (int i = 0; i < board.length; i += 3){
  23. for (int j = 0; j < board[0].length; j += 3){
  24. HashMap<Character, Integer> hashmap = new HashMap<Character, Integer>();
  25. for (int k = 0; k < 9; k++) {
  26. if (board[i + k / 3][j + k % 3] != '.') {
  27. if (hashmap.containsKey(board[i + k / 3][j + k % 3]))
  28. return false;
  29. hashmap.put(board[i + k / 3][j + k % 3], 1);
  30. }
  31. }
  32. }
  33. }
  34. return true;
  35. }

Java for LeetCode 036 Valid Sudoku的更多相关文章

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

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

  2. LeetCode 036 Valid Sudoku

    题目要求:Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudo ...

  3. LeetCode:36. Valid Sudoku,数独是否有效

    LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...

  4. Java [leetcode 36]Valid Sudoku

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

  5. 【LeetCode】036. Valid Sudoku

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

  6. 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]

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

  7. LeetCode 36 Valid Sudoku

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

  8. 【leetcode】Valid Sudoku

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

  9. LeetCode(38)-Valid Sudoku

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

随机推荐

  1. 【BZOJ-1965】SHUFFLE 洗牌 快速幂 + 拓展欧几里德

    1965: [Ahoi2005]SHUFFLE 洗牌 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 541  Solved: 326[Submit][St ...

  2. asp.net input怎么获取值

    前台: <input type="hidden" name="content" value="content"> 后台: Req ...

  3. POJ2492 A Bug's Life

    Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 33833   Accepted: 11078 Description Ba ...

  4. IIS FTP Server Anonymous Writeable Reinforcement, WEBDAV Anonymous Writeable Reinforcement(undone)

    目录 . 引言 . IIS 6.0 FTP匿名登录.匿名可写加固 . IIS 7.0 FTP匿名登录.匿名可写加固 . IIS >= 7.5 FTP匿名登录.匿名可写加固 . IIS 6.0 A ...

  5. POJ3233Matrix Power Series(十大矩阵问题之三 + 二分+矩阵快速幂)

    http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total ...

  6. jquery------添加jQuery对象方法

    my.js $(document).ready(function(){ (function($){ $.fn.swapClass=function(class1,class2){ if(this.ha ...

  7. Java多线程基础(一)

    一.基本概念 线程状态图包括五种状态 1.新建状态(New):线程对象被创建后,就进入新建状态.例如,Thread thread=new Thread(); 2.就绪状态(Runnable):也被称为 ...

  8. mac 下终端访问文件出现“Permission Denied”解决方案

    mac 下终端访问文件出现“Permission Denied”解决方案: 一个文件有3种权限,读.写.可执行,你这个文件没有可执行权限,需要加上可执行权限. 1. 终端下先 cd到该文件的目录下 2 ...

  9. PHP iconv函数字符串转码导致截断问题

    1.iconv函数原型 string iconv ( string $in_charset , string $out_charset , string $str ) in_charset:输入的字符 ...

  10. Android打电话&发短信

    public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView ...