0
点赞
收藏
分享

微信扫一扫

Leetcode self-record [day 3]

蛇发女妖 2022-02-18 阅读 73
leetcode

[Data Structure Study Plan] - Day 3

36. Valid Sudoku

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.

Solution:

class Solution:
    def testrepeat(self, testlists):
        t = list(digit for digit in testlists if digit.isdigit() == True)
        return len(list(set(t))) == len(t)
    
    def ifcol(self, board):
        for col in zip(*board):
            if not self.testrepeat(col):
                return False
        return True
    
    def ifrow(self, board):
        for row in board:
            if not self.testrepeat(row):
                return False
        return True
    
    def ifbox(self, board):
        for i in (0, 3, 6):
            for j in (0, 3, 6):
                box = [board[x][y] for x in range(i, i + 3) for y in range(j, j + 3)]
                if not self.testrepeat(box):
                    return False
        return True
    
    def isValidSudoku(self, board):
        return self.ifbox(board) == self.ifcol(board) == self.ifrow(board) == True
        

Remark:

Test three aspects: row, col, boxes(squares) --> 3 functions + 1 test function

In the Test function testrepeat, use the uniqueness property of set to test if there is repeated number. (so that you don't need to write for loop and if-else)

In the ifcol function, use zip(*iterated_item) to convert the row matrix to col matrix.

Feedback:

Runtime: 122 ms, faster than 55.87% of Python3 online submissions for Valid Sudoku.

Memory Usage: 13.9 MB, less than 95.73% of Python3 online submissions for Valid Sudoku.


74. Search a 2D Matrix

Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This matrix has the following properties:

  • Integers in each row are sorted from left to right.
  • The first integer of each row is greater than the last integer of the previous row.

Solution:  

class Solution:
    def searchMatrix(self, matrix, target):
        for row in matrix:
            row_origin = list(row)
            row.sort()
            if row != row_origin:
                return False

        resource = ()
        for col in zip(*matrix):
            resource += col
            col = list(col)
            col_origin = list(col)
            col.sort()
            if col != col_origin:
                return False

        if target not in resource:
            return False

        return True
        

Remark:

Seems not agile, but straightforward.

Feedback:

Runtime: 61 ms, faster than 48.49% of Python3 online submissions for Search a 2D Matrix.

Memory Usage: 14.4 MB, less than 82.31% of Python3 online submissions for Search a 2D Matrix.

To be continued... : )

举报

相关推荐

0 条评论