Leetcode 999. Available Captures for Rook

2021-07-13 14:39:58 浏览数 (1)

文章作者:Tyan 博客:noahsnail.com | CSDN | 简书

1. Description

2. Solution

**解析:**Version 1,先找到R,再分别统计四个方向上的p

  • Version 1
代码语言:javascript复制
class Solution:
    def numRookCaptures(self, board: List[List[str]]) -> int:
        length = 8
        x = -1
        y = -1
        for i in range(length):
            for j in range(length):
                if board[i][j] == 'R':
                    x = i
                    y = j
                    break
            if x != -1:
                break

        count = 0
        row = board[x]
        column = [board[i][y] for i in range(length)]
        count  = self.find(row, y-1, y 1, length)
        count  = self.find(column, x-1, x 1, length)
        return count


    def find(self, arr, i, j, length):
        count = 0
        while i > -1:
            if arr[i] == 'p':
                count  = 1
                break
            elif arr[i] == 'B':
                break
            i -= 1
        while j < length:
            if arr[j] == 'p':
                count  = 1
                break
            elif arr[j] == 'B':
                break
            j  = 1
        return count

Reference

  1. https://leetcode.com/problems/available-captures-for-rook/

0 人点赞