Leetcode Data Structure [36 | 74]

[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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值