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;
}
}

531. Lonely Pixel I的更多相关文章

  1. LeetCode 531. Lonely Pixel I

    原题链接在这里:https://leetcode.com/problems/lonely-pixel-i/ 题目: Given a picture consisting of black and wh ...

  2. [LeetCode] 531. Lonely Pixel I 孤独的像素 I

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

  3. [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 ...

  4. LeetCode 531. Longly Pixel I (孤独的像素之一) $

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

  5. [LeetCode] Lonely Pixel II 孤独的像素之二

    Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...

  6. [LeetCode] Lonely Pixel I 孤独的像素之一

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

  7. LeetCode 533. Lonely Pixel II (孤独的像素之二) $

    Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...

  8. 533. Lonely Pixel II

    Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...

  9. LeetCode Questions List (LeetCode 问题列表)- Java Solutions

    因为在开始写这个博客之前,已经刷了100题了,所以现在还是有很多题目没有加进来,为了方便查找哪些没加进来,先列一个表可以比较清楚的查看,也方便给大家查找.如果有哪些题目的链接有错误,请大家留言和谅解, ...

随机推荐

  1. shell脚本自动部署及监控

    一.shell脚本部署nginx反向代理和三个web服务 1 对反向代理服务器进行配置 #!/bin/bash #修改用户交互页面 用户输入参数执行相应的参数 #安装epel扩展包和nginx fun ...

  2. NoSuchBeanDefinitionException: No qualifying bean of type 'com.bj186.ssm.mapper.EmployeeMapper' available: expected at least 1 bean which qualifies as autowire candidate

    在搭建SSM spring springmvc  mybatis整合的时候, 遇到了这个问题 说说我的问题吧!我在进行单元测试的时候,出现了这个错误,网上一查才知道是,配置文件中没有写扫描包信息.一看 ...

  3. quartz测试类

    package demo.mytest; import java.text.ParseException; import org.quartz.CronTrigger;import org.quart ...

  4. class extension、class category、class-continuation category

    class extension Objective-C 2.0增加了class extensions用于解决两个问题: 允许一个对象可以拥有一个私有的interface,且可由编译器验证. 支持一个公 ...

  5. python3.x中的33个保留字

    Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32 Type " ...

  6. 《offline coolbook》笔记

    https://jakearchibald.com/2014/offline-cookbook/ 在install中对依赖进行缓存 self.addEventListener('install', f ...

  7. JS中关于clientWidth offsetWidth scrollWidth 等的含义的详细介绍

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  8. shell中test的使用

    #/secondin/secondfirstshecho “please enter two numseconder”read firstread secondif test $first -eq $ ...

  9. 杭电 5773 The All-purpose Zero

    Description ?? gets an sequence S with n intergers(0 < n <= 100000,0<= S[i] <= 1000000). ...

  10. 杭电 5748 Bellovin

    Description Peter has a sequence  and he define a function on the sequence -- , where  is the length ...