Java damn simple solution.


  • 2
    Z
    /**
     * === Count # of starting points. 
     * Starting point is X where left and up are empty.  
     */ 
    public class Solution {
        public int countBattleships(char[][] bd) {
            int count = 0;
            // count starting point. 
            for(int i =0; i < bd.length; i ++){
                for(int j =0; j <bd[i].length; j ++ ){
                    if(isStart(bd, i , j))
                        count ++;
                }
            }
            
            return count;
            
        }
        
        boolean isStart(char[][] bd, int i , int j){
            char up   = i -1 >= 0 ? bd[i-1][j] : '.';
            char left = j -1 >= 0 ? bd[i][j-1] : '.';
            
            return bd[i][j] == 'X' && left == '.' && up == '.';
        }
    }

  • 0
    S
    This post is deleted!

Log in to reply
 

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