Share my one line JavaScript solution.


  • 0
    G

    Use two times reduce method. one line, but hard to read :)

    
    var countBattleships = function(board) {
      return board.reduce((e,c,m)=>[].reduce.call(c, (i,j,n)=>(j === 'X' && (m === 0 || board[m - 1][n] !== 'X') && (n === 0 || c[n - 1] !== 'X')) ? ++i : i, e), 0)
    };
    

Log in to reply
 

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