531. Lonely Pixel I
2024-09-07 20:23:05
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:
- 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;
}
}
最新文章
- Game中的状态机
- 【转】MySQL中varchar最大长度是多少?
- Java线程:线程栈模型与线程的变量
- Java学习心得之 Linux下搭建JavaWeb环境
- 用canvas 实现个图片三角化(LOW POLY)效果
- [Ubuntu] Ubuntu DNS服务器配置
- 在windows下创建基于github的hexo静态博客
- ubuntu 经常使用软件及环境
- C#生成PDF页脚第几页共几页
- @Html.CheckBoxFor为何输出两种控件
- MyBatis报错:Caused by: java.lang.NumberFormatException: For input string: ";XX";
- selenium 定位元素成功, 但是输入失败 (textarea)
- System.TimeoutException: The operation requested on PersistentChannel timed out
- Regularity criteria for NSE 4: $\p_3u$
- python爬取微信信息--显示性别/地域/词云(附代码)
- linux下WEB服务器安装、配置VSFTP
- 一个简单的web.py论坛
- mysql创建账号及管理权限
- UGUI之控件以及按钮的监听事件系统
- 将本地Jar包安装到maven仓库中去
热门文章
- WPF使用附加属性绑定,解决data grid列绑定不上的问题
- Python基础篇 -- 列表
- 变色龙启动MAC时,错误信息“ntfs_fixup: magic doesn&#39;t match:”的解决办法
- 绑定用户id,用户权限认证
- (三)Python3 循环语句——while
- 我的Python分析成长之路4
- 如何完整反编译AndroidMainfest.xml
- Java-得到类的包
- 【20】display,float,position的关系
- Python基础数据类型之集合