zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    python之字典操作
    python之元组操作
    初始超算
    后缀自动机
    博弈
    曼哈顿最小生成树
    莫队算法
    主席树
    [HNOI2014]世界树
    [SDOI2011]消耗战
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6514404.html
Copyright © 2011-2022 走看看