Leetcode 999. Available Captures for Rook

Posted SnailTyan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 999. Available Captures for Rook相关的知识,希望对你有一定的参考价值。

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

1. Description

Available Captures for Rook

2. Solution

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

  • Version 1
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/

以上是关于Leetcode 999. Available Captures for Rook的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode --- 999. Available Captures for Rook 解题报告

Leetcode 999. Available Captures for Rook

leetcode 999. 车的可用捕获量(Available Captures for Rook)

Leetcode-999 Available Captures for Rook(车的可用捕获量)

LeetCode 258. Add Digits

leetcode999