Ruby O(1) space


  • 0
    Q
    def count_battleships(board)
        count = 0
        board.each_index do |i|
          board[i].each_index do |j|
            if board[i][j] == 'X'
              count += 1 if board[i][j+1] != 'X' && (board[i+1].nil? || board[i+1][j] != 'X')
            end
          end
        end
        count
      end
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.