Skip to content

Latest commit

 

History

History
24 lines (23 loc) · 579 Bytes

File metadata and controls

24 lines (23 loc) · 579 Bytes
  • 如果一个位置是 X,若其左侧和上侧也为 X,则该位置不是战舰头部
class Solution {
 public:
  int countBattleships(vector<vector<char>>& board) {
    if (empty(board) || empty(board[0])) {
      return 0;
    }
    int m = size(board);
    int n = size(board[0]);
    int res = 0;
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (board[i][j] == 'X' && (i == 0 || board[i - 1][j] != 'X') &&
            ((j == 0) || board[i][j - 1] != 'X')) {
          ++res;
        }
      }
    }
    return res;
  }
};