LeetCode 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 R and 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' pixel 的个数。 然后构造一个HashMap<String,Integer),对于那些 'B' 的个数有 N 个的行,将该行的 'WB' 序列当做一个字符串,统计这样的序列有多少。最后再对那些 'B' 的个数有 N 个的行,如果 HashMap 中该行的字符序列有 N 个,那么遍历该行所有的 'B' 所在的列,如果这一列有 N 个 'B',那么这一列的 N 个 'B' 就都是符合要求的 pixel了。 为了快速找到一行中的 'B' 所在的列,构造一个二维数组 int[][] rowIndexex=new int[rows][N],rowIndexes[i][j] 用于记录第 i 行第 j 个 'B' 所在的列数。
Java 算法实现:
public class Solution {
public int findBlackPixel(char[][] picture, int N) {
int rows=picture.length;
int cols=picture[0].length;
int []row=new int[rows];
int []col=new int[cols];
int [][]rowIndexes=new int[rows][N];//rowIndexes[i][j] 用于记录第 i 行第 j 个 'B' 所在的列数。
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
if(picture[i][j]=='B'){
if(row[i]!=-1&&row[i]!=N){
rowIndexes[i][row[i]]=j;//当前考察的'B'是第i行的第row[i]个'B'(从0开始计数),其所在列为 j 。
row[i]++;
}
else if(row[i]==N){// 第i行中的'B'的个数超过了N个,就将row[i]置为-1,以后就不用考虑该行了
row[i]=-1;
}
col[j]++;
}
}
}
Map<String, Integer>map=new HashMap<>();
for(int i=0;i<rows;i++){
String str=new String(picture[i]);//将这一行的'WB'序列当做一个字符串
if(map.containsKey(str)){//统计这中序列的个数,便于后面快速判断这一行是否符合要求(即有N个行的序列是这个序列)
int value=map.get(str)+1;
map.put(str, value);
}
else{
map.put(str, 1);
}
}
int ans=0;
for(int i=0;i<rows;i++){
if(row[i]==N){
String key=new String(picture[i]);
if(map.get(key)==N){//对于那些row[i]==N的行,其序列数目也为N,那么这一行上的'B'可能符合要求,下面进一步判断
for(int j=0;j<N;j++){
int jCol=rowIndexes[i][j];//jCol为第i行的'B'所在的列
if(col[jCol]==N){//这一列符合要求,那么这一列的N个'B'就都符合要求
ans+=N;
col[jCol]=-2;//后面跟这一列关联的行就不需要再进行重复计算了
}
else if(col[jCol]==-2){//说明与该行关联的含'B'的列之前已经在前面的行中进行统计过了,那么这一行就不必再统计了,直接退出本行的统计就可以了,提高算法时间效率
break;
}
}
}
}
}
return ans;
}
}
LeetCode 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 ...
- 533. 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 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
原题链接在这里:https://leetcode.com/problems/lonely-pixel-i/ 题目: Given a picture consisting of black and wh ...
- [LeetCode] Lonely Pixel I 孤独的像素之一
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- LeetCode 531. Longly Pixel I (孤独的像素之一) $
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
随机推荐
- [原创]PHP使用Redis实现Session共享
目录 前言 设计方案 1. 通过php自身session配置实现 2. 设置用户自定义会话存储函数 前言 小型web服务, session数据基本是保存在本地(更多是本地磁盘文件), 但是当部署多台服 ...
- python学习,day3:函数式编程,带参数
# coding=utf-8 # Author: RyAn Bi def test(x,y,z): print(x) print(y) print(z) test(y=2,z =3,x=1) #形参与 ...
- 基于Allwinner的Audio子系统分析(Android-5.1)
前言 一直想总结下Audio子系统的博客,但是各种原因(主要还是自己懒>_<),一直拖到现在才开始重新整理,期间看过H8(Android-4.4),T3(Android-4.4),A64( ...
- poj3250 Bad Hair Day 单调栈(递减)
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24420 Accepted: 8292 Des ...
- 关于MatlabGUI清除WorkSpace的用法
近日在调试Matlba GUI程序时,因为不想退出程序后手动Clear All来清理,又需要在过程中对WorkSpace进行清理,否则会引用之前的结果导致错误,找了很多资料,国内的论坛什么的都说用Cl ...
- springboot: mybatis的使用
第一步:引入mybatis依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifa ...
- 自定义 checkbox 样式
前言:最近在做一个网站,为了统一风格,需要自定义checkbox的样式.所以花了点时间参考了 研究了一下.感觉上面的方法略微麻烦.所以自己重新写了下面的代码,欢迎大家指教.同时,感谢w3cplus提供 ...
- http协议与他的三次握手和四次挥手
http:超文本传输协议,在客户端与服务端之间传输信息,客户端发送html,css给服务器,服务器返回源码给客户端: https:是一种更安全的传输协议,在协议上加了一层密码,不容易被黑客攻击,更加安 ...
- Java 中 String 的常用方法(二)
本文介绍剩下的一些常用的 String 中的方法. 1.replace 方法 .replaceFirst 方法和 replaceAll 方法 replace(char oldChar, char ne ...
- 剑指offer(11-20)编程题
二进制中1的个数 数值的整数次方 调整数组顺序使奇数位于偶数前面 链表中倒数第k个结点 反转链表 合并两个排序的链表 树的子结构 二叉树的镜像 顺时针打印矩阵 包含min函数的栈 11.输入一个整数, ...