zoukankan      html  css  js  c++  java
  • Valid Sudoku

    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.

    Subscribe to see which companies asked this question

    [分析]

    每行,每列,每个square,都不能重复.用 SET 记录即可. 

    [CODE]

    class Solution(object):
        def isValidSudoku(self, board):
            """
            :type board: List[List[str]]
            :rtype: bool
            """
            row,col,square = [],[],[]
            for i in range(10):
                row.append(set())
                col.append(set())
                square.append(set())
                
            
            for i in range(len(board)):
                for j in range(len(board)):
                    n = board[i][j]
                    if n=='.':continue
                    elif n<'0' or n>'9':return False
                    elif n in row[i] or n in col[j] or n in square[i/3*3+j/3]:
                        return False
                    else:
                        row[i].add(n)
                        col[j].add(n)
                        square[i/3*3+j/3].add(n)
            return True
                        
                        
    每天一小步,人生一大步!Good luck~
  • 相关阅读:
    java 字符串截取
    字符编码Unicode-正则表达式验证
    APP数据加密解密
    ThreadLocal线程局部变量
    用Eclipse进行远程Debug代码
    JPA对应关系
    JPA名称规则
    dubbo环境搭建
    历史表更新数据
    api加密算法
  • 原文地址:https://www.cnblogs.com/jkmiao/p/4939578.html
Copyright © 2011-2022 走看看