首页 技术 正文
技术 2022年11月16日
0 收藏 454 点赞 4,834 浏览 1772 个字

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 indexTake '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 "";
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,983
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,500
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,344
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,127
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,762
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,838