533. Lonely Pixel II
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:
- Row R and column C both contain exactly N black pixels.
- 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:
- 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的更多相关文章
- [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 533. 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 II 孤独的像素之二
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- [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] Lonely Pixel I 孤独的像素之一
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- 531. Lonely Pixel I
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- LeetCode 531. Lonely Pixel I
原题链接在这里:https://leetcode.com/problems/lonely-pixel-i/ 题目: Given a picture consisting of black and wh ...
- LeetCode 533----Lonely Pixel II
问题描述 Given a picture consisting of black and white pixels, and a positive integer N, find the number ...
- LeetCode Questions List (LeetCode 问题列表)- Java Solutions
因为在开始写这个博客之前,已经刷了100题了,所以现在还是有很多题目没有加进来,为了方便查找哪些没加进来,先列一个表可以比较清楚的查看,也方便给大家查找.如果有哪些题目的链接有错误,请大家留言和谅解, ...
随机推荐
- go语音实战读后感——一
1.第一个go程序: package main import ( "fmt" ) func main() { fmt.Println("Hello go") } ...
- java项目指向maven进行构建方式
1.在需要运行的机器中环境变量中配置maven 运行setting 4 配置环境变量 2.运行项目进行重新构建:alt+F5
- 多数据源连接Oracle报错,linux熵池耗尽问题
最近碰到了个很有意思的问题,springboot加载多数据源,遇到了在启动时数据库连接报错的问题. 报错信息: The error occurred while executing a query 然 ...
- 常用JavaScript正则表达式整理
在表单验证中,正则表达式书写起来特别繁琐,本文整理了15个常用的JavaScript正则表达式,其中包括用户名.密码强度.整数.数字.电子邮件地址(Email).手机号码.身份证号.URL地址. IP ...
- js和JQuery中的获取宽、高、位置等方法整理
1.获取当前窗口宽度区别(需要注意的是用的window还是document)JQuery:console.log($(window).width()); //获取窗口可视区域的宽度 console.l ...
- Java--返回类的对象(return this)
如下代码所示: public Book getBook(){ return this; } 在getBook()方法中,方法的返回值为Book类,所以方法体中使用 return this 这种形式返回 ...
- Web鼠标事件
mousedown:鼠标按下 mouseup:鼠标抬起 mousemove:鼠标移动 mouseout:在父元素上绑定该事件,当鼠标从父元素或者从子元素上离开时都会触发该事件 mouseleave:和 ...
- Python9-面对对象2-day23
#计算正方形的周长和面积 class Square: def __init__(self,side_len): self.side_len = side_len def perimeter(self) ...
- Spring,Mybatis,Springmvc框架整合项目(第一部分)
一.说在前面的话 本篇博文实现一个注册登录小项目,使用spring,mybatis,springmvc框架进行整合,我们创建的是一个maven工程,主要是方便jar包版本的管理.项目使用eclispe ...
- PAT Basic 1039
1039 到底买不买 小红想买些珠子做一串自己喜欢的珠串.卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖.于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么 ...