Day 10 - Valid Sudoku

大家好,我是毛毛。ヾ(´∀ ˋ)ノ
废话不多说开始今天的解题Day~

tags: 2021_iThome铁人赛 Leetcode

36. Valid Sudoku

Question

Determine if a 9 x 9 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 nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.

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.

Example

Example1

Input: board = 
[["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

Example2

Input: board = 
[["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.

Constraints

  • board.length == 9
  • board[i].length == 9
  • board[i][j] is a digit 1-9 or '.'.

解题

题目

首先先简单的翻译一下题目
给一个数独题目要你解,给一个数独题目要判断这个数独题目是不是合乎数独的规则,也就是不管是行、列和9块3 x 3的区块,出现的数字1-9皆不能重复。

Think

作法大致上是这样

  • 本来一开始是打算用for loop做,但是忽然想到有enumerate可以使用,enumerate有点像for rowVal in board:的方式,只是每个值会在额外配上一个sequence,这个sequence的起始可以透过start参数来决定。
  • 因为有行、列和区块要判断,所以就分三个部份分别处理。
  • 下面避免行列混淆,用RowColumn来说明。
  • Row part & Column part
    • rowcol这两个list存着已经出现过的值,假如新读到的值已经有存在里头了,就代表重复了,回传False;没有的话,就存入。
    • 存入的值为(seq, value)搭配
  • subBox part
    • subBox这个list存着已经出现过的值,假如新读到的值已经有存在里头了,就代表重复了,回传False;没有的话,就存入。
    • 存入的值是取seq/3的商,这边我用转int把小数部分舍去了。
    • 这样可以得到9个区块分别的代码:
      • 0,00,10,2
      • 1,01,11,2
      • 2,02,12,2
    • 在配上每个点的value就可以透过subBox的list判断有没有重复数字了。
  • C的程序出了点问题QQ,再补上。

Code

Python

class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        row = []
        col = []
        subBox = []
        
        for seq1, rowVal in enumerate(board, start=0):
            for seq2, colVal in enumerate(rowVal, start=0):

                if colVal is not ".":
                    # Row part
                    if (seq1, colVal) not in row:
                        row.append((seq1, colVal))
                    else:
                        return False

                    # Column part
                    if (seq2, colVal) not in col:
                        col.append((seq2, colVal))
                    else:
                        return False
                
                    # subBox part
                    if (int(seq1/3), int(seq2/3), colVal) not in subBox:
                        subBox.append((int(seq1/3), int(seq2/3), colVal))
                    else:
                        return False
        return True

C


Result

  • Python

  • C

大家明天见/images/emoticon/emoticon29.gif


<<:  [DAY 10] 大口香鸡排

>>:  Day-11: form_for 系虾密?很好用!

资安学习路上- Injection的爱恨情仇5

语法拼接 前面Injection的爱恨情仇4讲到SQL injection常发生在语法拼接的地方,这...

2022 年 5 大蓝光 ISO 播放器软件

大多数普通媒体播放器都无法播放蓝光ISO档、蓝光光盘以及BD资料夹。在这篇文章中,我将重点介绍市场上...

Day 0xE - 建立订单纪录到资料库

0x1 前言 目前还没有画面可以看或操作,所以单就资料流的部分先写进资料库, 而建立订单目前也是先塞...

Day-7 带着童年的好朋友任天堂红白机、重新在 HDMI 电视上发光吧!

写了好几天的事前准备、我想大家应该都腻了。终於、准备到了一定程度、可以进入本文了。这篇文章主要的目的...

[Day 12] Forensics 小挑战

今天心情蛮好的,期待叻2周终於等到这天了,生平第一次染发:) 这篇文有一半是我边染发变打出来的~ 染...