Given a picture consisting of black and white pixels, and a positive integer N, find the number of black pixels located at some specific row Rand column C that align with all the following rules:

  1. Row R and column C both contain exactly N black pixels.
  2. For all rows that have a black pixel at column C, they should be exactly the same as row R

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

Example:

Input:
[['W', 'B', 'W', 'B', 'B', 'W'],
['W', 'B', 'W', 'B', 'B', 'W'],
['W', 'B', 'W', 'B', 'B', 'W'],
['W', 'W', 'B', 'W', 'B', 'W']] N = 3
Output: 6
Explanation: All the bold 'B' are the black pixels we need (all 'B's at column 1 and 3).
0 1 2 3 4 5 column index
0 [['W', 'B', 'W', 'B', 'B', 'W'],
1 ['W', 'B', 'W', 'B', 'B', 'W'],
2 ['W', 'B', 'W', 'B', 'B', 'W'],
3 ['W', 'W', 'B', 'W', 'B', 'W']]
row index Take 'B' at row R = 0 and column C = 1 as an example:
Rule 1, row R = 0 and column C = 1 both have exactly N = 3 black pixels.
Rule 2, the rows have black pixel at column C = 1 are row 0, row 1 and row 2. They are exactly the same as row R = 0.

Note:

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

本题读了好几遍题目也没有怎么读懂,有点小难了,那两个限制条件的大致意思是,第一,某一点为B的点,它的行和列的B的个数都是N,第二个意思是,每一行里面出现的B,B的整个列为B的行必须和该B的行的字符顺序是一样的,代码如下:

 public class Solution {
public int findBlackPixel(char[][] picture, int N) {
int row = picture.length;
int col = picture[0].length;
int[] colcount =new int[col];
Map<String,Integer> map = new HashMap<>();
for(int i=0;i<row;i++){
String s = scanRow(picture,N,colcount,i);
if(s.length()!=0)
map.put(s,map.getOrDefault(s,0)+1);
}
int res = 0;
for(String key:map.keySet()){
if(map.get(key)==N){
for(int i=0;i<col;i++){
if(key.charAt(i)=='B'&&colcount[i]==N){
res+=N;
}
}
}
}
return res; }
public String scanRow(char[][] picture,int N,int[] colcount,int row){
StringBuilder sb = new StringBuilder();
int col = picture[0].length;
int count = 0;
for(int i=0;i<col;i++){
if(picture[row][i]=='B'){
count++;
colcount[i]++;
}
sb.append(picture[row][i]);
}
if(count==N) return sb.toString();
else return "";
}
}

533. Lonely Pixel II的更多相关文章

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

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

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

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

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

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

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

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

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

  6. 531. Lonely Pixel I

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

  7. LeetCode 531. Lonely Pixel I

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

  8. LeetCode 533----Lonely Pixel II

    问题描述 Given a picture consisting of black and white pixels, and a positive integer N, find the number ...

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

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

随机推荐

  1. shell脚本,awk 根据文件某列去重并且统计该列频次。

    a文件为 a a a s s d .怎么把a文件变为 a s d .怎么把a文件变为 a a a s s d 解题方法如下: 解题思路 [root@localhost study]# awk 'NR= ...

  2. 使用custom elements和Shadow DOM自定义标签

    具体的api我就不写 官网上面多  如果不知道这个的话也可以搜索一下 目前感觉这个还是相当好用的直接上码. <!DOCTYPE html> <html lang="en&q ...

  3. CF-1100 E Andrew and Taxi

    CF-1100E Andrew and Taxi https://codeforces.com/contest/1100/problem/E 知识点: 二分 判断图中是否有环 题意: 一个有向图,每边 ...

  4. 使用Spring AOP实现业务依赖解耦

    Spring IOC用于解决对象依赖之间的解耦,而Spring AOP则用于解决业务依赖之间的解耦: 统一在一个地方定义[通用功能],通过声明的方式定义这些通用的功能以何种[方式][织入]到某些[特定 ...

  5. 【DB_MySQL】查询语句中各子句的执行顺序

    1. FROM 指明查询来源 2. WHERE筛选元组 3. GROUP BY进行分组 4. HAVING 筛选分组 5. SELECT 投影出指定的字段列 6. ORDER BY 对结果集排序 7. ...

  6. MySQL学习点滴

    MySQL学习点滴 --分区表 概述: 分区功能并不是在存储引擎层完成的,因此很多存储引擎包括InnoDB, MyISAM, NDB等都支持分区功能.但也并不是所有的存储引擎都支持分区.在使用分区前, ...

  7. idea导入jdk源码查看(xjl456852原创)

    idea添加了jdk环境后,却无法查看jdk源码,只能通过idea自带的反编译查看,看起来有些不爽. 下面来说一下如何设置,导入jdk源码,查看时通过源码查看jdk. 1.点击菜单 File -> ...

  8. Python模块--time&datetime

    一.Python中时间的表示方式 1.时间戳  如 1552623413.043036 2.格式化的时间字符串  如 2015-12-02 3.struct_time  是一个元组 共有九个元素 二. ...

  9. skkyk:线段树浅谈

    推荐前辈学姐博客文章,写的很细 https://www.cnblogs.com/TheRoadToTheGold/p/6254255.html 学学半,此随笔主要是加深自己对线段树的理解 题目:洛谷P ...

  10. mysql远程访问另一台主机数据库表,实现小表广播功能

    1.打开navicat,打开任意一个连接,新建一个查询,输入命令 show engines,出现如下界面 2. 如果FEDERATED对应的Support值为NO,则找到C:\ProgramData\ ...