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

验证当前九宫格盘面是否有错误

  1. class Solution:
  2. def valid3by3(self, board, row, col):
  3. validPos = [
  4. [-1, -1], [-1, 0], [-1, 1],
  5. [0, -1], [0, 0], [0, 1],
  6. [1, -1], [1, 0], [1, 1]
  7. ]
  8. s = set()
  9. for pos in validPos:
  10. curVal = board[row + pos[0]][col + pos[1]]
  11. if curVal is ".":
  12. continue
  13. if curVal in s:
  14. return False
  15. s.add(curVal)
  16. return True
  17. def validRow(self, board, row):
  18. s = set()
  19. for curVal in board[row]:
  20. if curVal is ".":
  21. continue
  22. if curVal in s:
  23. return False
  24. s.add(curVal)
  25. return True
  26. def validCol(self, board, col):
  27. s = set()
  28. for row in board:
  29. curVal = row[col]
  30. if curVal is ".":
  31. continue
  32. if curVal in s:
  33. return False
  34. s.add(curVal)
  35. return True
  36. def isValidSudoku(self, board):
  37. """
  38. :type board: List[List[str]]
  39. :rtype: bool
  40. """
  41. pos3by3 = [
  42. [1, 1], [1, 4], [1, 7],
  43. [4, 1], [4, 4], [4, 7],
  44. [7, 1], [7, 4], [7, 7]
  45. ]
  46. for pos in pos3by3:
  47. if not self.valid3by3(board, pos[0], pos[1]):
  48. return False
  49. for row in range(0, 9):
  50. if not self.validRow(board, row):
  51. return False
  52. for col in range(0, 9):
  53. if not self.validCol(board, col):
  54. return False
  55. return True
  56. s = Solution()
  57. board = [
  58. [".", "8", "7", "6", "5", "4", "3", "2", "1"],
  59. ["2", ".", ".", ".", ".", ".", ".", ".", "."],
  60. ["3", ".", ".", ".", ".", ".", ".", ".", "."],
  61. ["4", ".", ".", ".", ".", ".", ".", ".", "."],
  62. ["5", ".", ".", ".", ".", ".", ".", ".", "."],
  63. ["6", ".", ".", ".", ".", ".", ".", ".", "."],
  64. ["7", ".", ".", ".", ".", ".", ".", ".", "."],
  65. ["8", ".", ".", ".", ".", ".", ".", ".", "."],
  66. ["9", ".", ".", ".", ".", ".", ".", ".", "."]
  67. ]
  68. res = s.isValidSudoku(board)
  69. print(res)

来自为知笔记(Wiz)

原文地址:https://www.cnblogs.com/xiejunzhao/p/8319163.html

时间: 2024-11-08 15:17:52

36. Valid Sudoku 有效的数独的相关文章

leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法

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 boa

LeetCode 36 Valid Sudoku(合法的数独)

题目链接: https://leetcode.com/problems/valid-sudoku/?tab=Description 给出一个二维数组,数组大小为数独的大小,即9*9 其中,未填入数字的数组值为’.’ 判断当前所给已知数组中所填的数字是否合法. 数独合法性判断: 1. 满足每一行的数字都只能是1~9,并且不能产生重复 2. 满足每一列的数字都只能是1~9,并且不能产生重复 3. 满足每一个3*3的正方形块中的数字只能是1~9,并且不能产生重复 判断过程: 初始化三个数组:row,

leetCode 36. Valid Sudoku(数独) 哈希

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

[Leetcode][Python]36: Valid Sudoku

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 36: Valid Sudokuhttps://oj.leetcode.com/problems/valid-sudoku/ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty

LeetCode 36 Valid Sudoku (C,C++,Java,Python)

Problem: 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 (

【LeetCode】36 - Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRules.aspx) The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. N

[email protected] [36] Valid Sudoku

https://leetcode.com/problems/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

36. Valid Sudoku/37. Sudoku Solver - 数独问题-- backtracking 经典

题意: 经典的递归题, 要求:除了要求 横竖都填满 1~9外, 每个3*3也都要求满足 1~9 36. 数组可以部分填充, 问是否一个有效的 sudoku. 写了个好烧脑的 四重循环来check 3*3 的部分.  重点在于 用数组作为hash . 然后对于 check 3*3 部分, 其实就是9个小方块,  9个小方块 定点坐标为 [0 0] [0 3] [06] [3 0] [3 3 ] [3 6] [6 0] [ 6 3] [6 6] ,每个顶点递增为横竖 递增都 3 , 因此写了个二重循

LeetCode OJ: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 '.'. 注意这里的有效数独并非指的是可以解出来,只要存在的数满足数独的条件就可以了. 原理很简单,但是判定在同一个blocks的时候出了点问题,没想到判定方法,看了下