zoukankan      html  css  js  c++  java
  • 【leetcode】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.

    解题思路:

    把三个都判断下就完了

    class Solution:
        # @param board, a 9x9 2D array
        # @return a boolean
        def isValidSudoku(self, board):
            #row
            for i in range(9):
                vis = [False] * 9
                for j in range(9):
                    if board[i][j] == '.':
                        continue
                    if vis[int(board[i][j])-1] == True:
                        return False
                    else:
                        vis[int(board[i][j])-1] = True
            
            #colum
            for i in range(9):
                vis = [False] * 9
                for j in range(9):
                    if board[j][i] == '.':
                        continue
                    if vis[int(board[j][i])-1] == True:
                        return False
                    else:
                        vis[int(board[j][i])-1] = True
                        
            #block
            for i in range(0,9,3):
                for j in range(0,9,3):
                    vis = [False] * 9
                    for k in range(i,3+i):
                        for l in range(j,3+j):
                            if board[k][l] == '.':
                                continue
                            if vis[int(board[k][l])-1] == True:
                                return False
                            else:
                                vis[int(board[k][l])-1] = True
                                
            return True
  • 相关阅读:
    Array 数组对象
    Math对象
    String 字符串对象
    Date 日期对象
    一个简单的计算器
    如何判断一个js对象是否一个DOM对象
    筛选if 运用
    移动端前端笔记大全
    一个元素的偏移的方法
    如果判断一个dom 对像?
  • 原文地址:https://www.cnblogs.com/MrLJC/p/4438407.html
Copyright © 2011-2022 走看看