Given a picture consisting of black and white pixels, find the number of black lonely pixels.

The picture is represented by a 2D char array consisting of 'B' and 'W', which means black and white pixels respectively.

A black lonely pixel is character 'B' that located at a specific position where the same row and same column don't have any other black pixels.

Example:

Input:
[['W', 'W', 'B'],
['W', 'B', 'W'],
['B', 'W', 'W']] Output: 3
Explanation: All the three 'B's are black lonely pixels.

Note:

  1. The range of width and height of the input 2D array is [1,500].

本题有点类似于皇后问题,思路是,第一遍找出有B的字符,然后把它的行和列分别+1,第二遍遍历的时候,找出有B的字符并且,行和列都是1的字符,count++;

代码如下:

 public class Solution {
public int findLonelyPixel(char[][] picture) {
int count = 0;
int row = picture.length;
int col = picture[0].length;
int[] rows = new int[row];
int[] cols = new int[col];
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(picture[i][j]=='B'){
rows[i]++;
cols[j]++;
}
}
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(picture[i][j]=='B'&&rows[i]==1&&cols[j]==1){
count++;
}
}
}
return count;
}
}

最新文章

  1. Game中的状态机
  2. 【转】MySQL中varchar最大长度是多少?
  3. Java线程:线程栈模型与线程的变量
  4. Java学习心得之 Linux下搭建JavaWeb环境
  5. 用canvas 实现个图片三角化(LOW POLY)效果
  6. [Ubuntu] Ubuntu DNS服务器配置
  7. 在windows下创建基于github的hexo静态博客
  8. ubuntu 经常使用软件及环境
  9. C#生成PDF页脚第几页共几页
  10. @Html.CheckBoxFor为何输出两种控件
  11. MyBatis报错:Caused by: java.lang.NumberFormatException: For input string: &quot;XX&quot;
  12. selenium 定位元素成功, 但是输入失败 (textarea)
  13. System.TimeoutException: The operation requested on PersistentChannel timed out
  14. Regularity criteria for NSE 4: $\p_3u$
  15. python爬取微信信息--显示性别/地域/词云(附代码)
  16. linux下WEB服务器安装、配置VSFTP
  17. 一个简单的web.py论坛
  18. mysql创建账号及管理权限
  19. UGUI之控件以及按钮的监听事件系统
  20. 将本地Jar包安装到maven仓库中去

热门文章

  1. WPF使用附加属性绑定,解决data grid列绑定不上的问题
  2. Python基础篇 -- 列表
  3. 变色龙启动MAC时,错误信息“ntfs_fixup: magic doesn&#39;t match:”的解决办法
  4. 绑定用户id,用户权限认证
  5. (三)Python3 循环语句——while
  6. 我的Python分析成长之路4
  7. 如何完整反编译AndroidMainfest.xml
  8. Java-得到类的包
  9. 【20】display,float,position的关系
  10. Python基础数据类型之集合