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 ‘B‘ that located at a specific position where the same row and same column don‘t have any other black pixels.

Example:

Input:
[[‘W‘, ‘W‘, ‘B‘],
 [‘W‘, ‘B‘, ‘W‘],
 [‘B‘, ‘W‘, ‘W‘]]

Output: 3
Explanation: All the three ‘B‘s are black lonely pixels.

Note:

  1. The range of width and height of the input 2D array is [1,500].

本题有点类似于皇后问题,思路是,第一遍找出有B的字符,然后把它的行和列分别+1,第二遍遍历的时候,找出有B的字符并且,行和列都是1的字符,count++;

代码如下:

 1 public class Solution {
 2     public int findLonelyPixel(char[][] picture) {
 3         int count = 0;
 4         int row = picture.length;
 5         int col = picture[0].length;
 6         int[] rows = new int[row];
 7         int[] cols = new int[col];
 8         for(int i=0;i<row;i++){
 9             for(int j=0;j<col;j++){
10                 if(picture[i][j]==‘B‘){
11                     rows[i]++;
12                     cols[j]++;
13                 }
14             }
15         }
16         for(int i=0;i<row;i++){
17             for(int j=0;j<col;j++){
18                 if(picture[i][j]==‘B‘&&rows[i]==1&&cols[j]==1){
19                     count++;
20                 }
21             }
22         }
23         return count;
24     }
25 }
时间: 2025-01-05 05:51:54

531. Lonely Pixel I的相关文章

[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

LeetCode 531. Longly 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

[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

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

过中等难度题目.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