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:

  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的行的字符顺序是一样的,代码如下:

 1 public class Solution {
 2     public int findBlackPixel(char[][] picture, int N) {
 3         int row = picture.length;
 4         int col = picture[0].length;
 5         int[] colcount  =new int[col];
 6         Map<String,Integer> map = new HashMap<>();
 7         for(int i=0;i<row;i++){
 8             String s = scanRow(picture,N,colcount,i);
 9             if(s.length()!=0)
10                 map.put(s,map.getOrDefault(s,0)+1);
11         }
12         int res = 0;
13         for(String key:map.keySet()){
14             if(map.get(key)==N){
15                 for(int i=0;i<col;i++){
16                     if(key.charAt(i)==‘B‘&&colcount[i]==N){
17                         res+=N;
18                     }
19                 }
20             }
21         }
22         return res;
23
24     }
25     public String scanRow(char[][] picture,int N,int[] colcount,int row){
26         StringBuilder sb = new StringBuilder();
27         int col = picture[0].length;
28         int count = 0;
29         for(int i=0;i<col;i++){
30             if(picture[row][i]==‘B‘){
31                 count++;
32                 colcount[i]++;
33             }
34             sb.append(picture[row][i]);
35         }
36         if(count==N) return sb.toString();
37         else return "";
38     }
39 }
时间: 2024-11-05 14:39:49

533. Lonely Pixel II的相关文章

[LeetCode] 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 pixel

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

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

Lonely Pixel I

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

531. Lonely Pixel I

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

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

【LeetCode】数组

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum [4]Median of Two Sorted Arrays [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [26]Remove Duplicates from Sorted Array [27]Remove Element [31]Next Permutatio