题意:

每个连续的x成为一组,每一组x独立,求x组的个数

思路:

双指针 复杂度O(row*col)

code java

class Solution {
public int countBattleships(char[][] board) {

int n=board.length;
if(n==0) return 0;
int m=board[0].length;
boolean st[][]=new boolean [n][m];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++) st[i][j]=false;
int res=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++)
{
if(st[i][j]) continue;
if(board[i][j]=='X')
{
res++;
int k=j;
while(k<m)
{
if(board[i][k]=='.') break;
st[i][k]=true;
k++;
}
k=i;
while(k<n){
if(board[k][j]=='.') break;
st[k][j]=true;
k++;
}
}
}
}
return res;
}
}