531. Lonely Pixel I
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;
}
}
531. Lonely Pixel I的更多相关文章
- LeetCode 531. Lonely Pixel I
原题链接在这里:https://leetcode.com/problems/lonely-pixel-i/ 题目: Given a picture consisting of black and wh ...
- [LeetCode] 531. Lonely Pixel I 孤独的像素 I
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- [LeetCode] 533. Lonely Pixel II 孤独的像素 II
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- LeetCode 531. Longly Pixel I (孤独的像素之一) $
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- [LeetCode] Lonely Pixel II 孤独的像素之二
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- [LeetCode] Lonely Pixel I 孤独的像素之一
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- LeetCode 533. Lonely Pixel II (孤独的像素之二) $
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- 533. Lonely Pixel II
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- LeetCode Questions List (LeetCode 问题列表)- Java Solutions
因为在开始写这个博客之前,已经刷了100题了,所以现在还是有很多题目没有加进来,为了方便查找哪些没加进来,先列一个表可以比较清楚的查看,也方便给大家查找.如果有哪些题目的链接有错误,请大家留言和谅解, ...
随机推荐
- Mac终端(Terminal)自定义颜色,字体,背景
使用Mac作为开发机的时候,苹果终端自带的颜色黑白,字体又小,看起来确实不是很舒服.那推荐大家使用Solarized配色方案.Solarized 是目前最完整的 Terminal/Editor/IDE ...
- CPP-基础:关于多态
类的多态特性是支持面向对象的语言最主要的特性,有过非面向对象语言开发经历的人,通常对这一章节的内容会觉得不习惯,因为很多人错误的认为,支持类的封装的语言就是支持面向对象的,其实不然,Visua ...
- Codeforces Round #271 (Div. 2)-B. Worms
http://codeforces.com/problemset/problem/474/B B. Worms time limit per test 1 second memory limit pe ...
- struts2的多个文件上传
成功效果图: 上篇文章描述了单个文件的上传和配置,下面主要讲解下不同的地方: index.jsp <head> <script ...
- javaEE(6)_JSP
一.什么是JSP 1.JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术,只用JSP就可以开发动态web资源. 2.为什么J ...
- makeObjectsPerformSelector用法
亲测 makeObjectsPerformSelector 的用法. - (void)makeObjectsPerformSelector:(SEL)aSelector NS_SWIFT_UNAVAI ...
- ExtJs如何使用自定义插件动态保存表头配置(隐藏或显示)
关于保存列表表头的配置,一般我们不需要与后台交互,直接保存在 localStorage 中就能满足常规使用需求(需要浏览器支持). 直接上代码,插件: Ext.define('ux.plugin.Co ...
- angular 列表渲染机制
watchCollection:监听集合元素的变化,而不能监听到集合元素内部的属性变化,只要集合中元素的引用没有发生变化,则认为无变化.用这个api也可以监听普通对象的第一层属性变化. watch:监 ...
- verilog disable 用法 (易错!)
disable语句可以退出任何循环,能够终止任何begin..end块的执行,用于仿真验证中. 例如 begin:one ;i<;i=i+) begin:two ) disable one; / ...
- 杭电 1503 Advanced Fruits
Description The company "21st Century Fruits" has specialized in creating new sorts of fru ...