419.甲板上的战舰

遍历

C++

class Solution {
public:
    int countBattleships(vector<vector<char>>& board) {
        int m = board.size();
        int n = board[0].size();
        int res = 0;
        for (int i = 0; i < m; i++){
            for (int j = 0; j < n; j++){
                if (board[i][j] == 'X'){
                    board[i][j] = '.';
                    // 将之后这一行的所有列置为.
                    for (int k = j+1; k < n && board[i][k] == 'X'; k++){
                        board[i][k] = '.';
                    }
                    for (int k = i+1; k < m && board[k][j] == 'X'; k++){
                        board[k][j] = '.';
                    }
                    res ++;
                }
            }
        }
        return res;
    }
};

Python

class Solution:
    def countBattleships(self, board: List[List[str]]) -> int:
        m, n = len(board), len(board[0])
        res = 0
        for i in range(0,m):
            for j in range(0, n):
                if board[i][j] == 'X':
                    board[i][j] = '.'
                    for k in range(j+1, n):
                        if board[i][k] != 'X':
                            break
                        board[i][k] = '.'
                    for k in range(i+1, m):
                        if board[k][j] != 'X':
                            break
                        board[k][j] = '.'
                    res += 1
        return res

枚举起点

发现, 要么行固定,要么列固定,可以发现如果当前位置左上定点都为’X’,则不满足条件。所以如果当前位置为’X’,判断其左上定点是否也是’X’,如果是则不满足。

C++

class Solution {
public:
    int countBattleships(vector<vector<char>>& board) {
        int m = board.size();
        int n = board[0].size();
        int res = 0;
        for (int i = 0; i < m; i++){
            for (int j = 0; j < n; j++){
                if (board[i][j] == 'X'){
                    if (i > 0 && board[i-1][j] == 'X'){
                        continue;
                    }
                    if (j > 0 && board[i][j-1] == 'X'){
                        continue;
                    }
                    res++;
                }
            }
        }
        return res;
    }
};

Python

class Solution:
    def countBattleships(self, board: List[List[str]]) -> int:
        return sum(ch == 'X' and not(i > 0 and board[i-1][j] == 'X' or j > 0 and board[i][j-1]=='X') for i, row in enumerate(board) for j, ch in enumerate(row))

相关推荐

  1. 419.甲板战舰

    2024-06-12 00:16:01       31 阅读
  2. Leetcode459:重复字符串

    2024-06-12 00:16:01       64 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-12 00:16:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-12 00:16:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-12 00:16:01       82 阅读
  4. Python语言-面向对象

    2024-06-12 00:16:01       91 阅读

热门阅读

  1. 比亚迪算法岗面试,问的贼细

    2024-06-12 00:16:01       34 阅读
  2. Python中的可变参数

    2024-06-12 00:16:01       24 阅读
  3. 问题 B: 2.左右(lr.cpp/pas)

    2024-06-12 00:16:01       41 阅读
  4. Vue小细节

    2024-06-12 00:16:01       40 阅读
  5. VPN简介

    2024-06-12 00:16:01       34 阅读
  6. C语言与内存息息相关的重要概念有哪些?

    2024-06-12 00:16:01       30 阅读
  7. 超宽输送带的最适合的应用领域是什么

    2024-06-12 00:16:01       28 阅读