LeetCode: Battleships in a Board

发布时间:2017-2-20 9:46:31 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"LeetCode: Battleships in a Board ",主要涉及到LeetCode: Battleships in a Board 方面的内容,对于LeetCode: Battleships in a Board 感兴趣的同学可以参考一下。

Battleships之间并不重叠这个assumption很关键,因此只要看船头就可以判断是不是有一个船

 1 public class Solution { 2     public int countBattleships(char[][] board) { 3         int ans = 0; 4         for (int i = 0; i < board.length; i++) { 5             for (int j = 0; j < board[0].length; j++) { 6                 if (board[i][j] == 'X' && ((i != 0 && board[i-1][j] != 'X') || i == 0) && ((j != 0 && board[i][j-1] !='X') || j == 0)) 7                     ans++; 8             } 9         }10         return ans;11     }12 }

上一篇:Unity3D研究院编辑器之脚本设置ToolBar
下一篇:【MFC】MFC中使对话框变成圆角矩形的代码(转)

相关文章

相关评论