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].

给一个只含有黑白像素的图片,找出黑色孤独像素的数量。黑色孤独像素是这个像素所在的行和列都不含有黑色像素。

最基本的想法就是找出每一个黑色像素,然后对相应的行和列进行检查,看是否含有黑色像素。但这种方法肯定含有重复操作,效率肯定不高。

解法:利用数组rows,cols分别记录某行、某列'B'像素的个数。然后遍历一次picture找到符合条件的。

Java:

public int findLonelyPixel(char[][] picture) {
int n = picture.length, m = picture[0].length; int[] rowCount = new int[n], colCount = new int[m];
for (int i=0;i<n;i++)
for (int j=0;j<m;j++)
if (picture[i][j] == 'B') { rowCount[i]++; colCount[j]++; } int count = 0;
for (int i=0;i<n;i++)
for (int j=0;j<m;j++)
if (picture[i][j] == 'B' && rowCount[i] == 1 && colCount[j] == 1) count++; return count;
}

Java: DFS

public int findLonelyPixel(char[][] picture) {
int numLone = 0;
for (int row = 0; row < picture.length; row++) {
for (int col = 0; col < picture[row].length; col++) {
if (picture[row][col] == 'W') {
continue;
}
if (dfs(picture, row - 1, col, new int[] {-1, 0}) && dfs(picture, row + 1, col, new int[] {1, 0})
&& dfs(picture, row, col - 1, new int[] {0, -1}) && dfs(picture, row, col + 1, new int[] {0, 1})) {
numLone++;
}
}
}
return numLone;
} // use dfs to find if current pixel is lonely
private boolean dfs(char[][] picture, int row, int col, int[] increase) {
// base case
if (row < 0 || row >= picture.length || col < 0 || col >= picture[0].length) {
return true;
} else if (picture[row][col] == 'B') {
return false;
}
// recursion
return dfs(picture, row + increase[0], col + increase[1], increase);
}    

Python:

# Time:  O(m * n)
# Space: O(m + n)
class Solution(object):
def findLonelyPixel(self, picture):
"""
:type picture: List[List[str]]
:rtype: int
"""
rows, cols = [0] * len(picture), [0] * len(picture[0])
for i in xrange(len(picture)):
for j in xrange(len(picture[0])):
if picture[i][j] == 'B':
rows[i] += 1
cols[j] += 1 result = 0
for i in xrange(len(picture)):
if rows[i] == 1:
for j in xrange(len(picture[0])):
result += picture[i][j] == 'B' and cols[j] == 1
return result

Python:

class Solution(object):
def findLonelyPixel(self, picture):
"""
:type picture: List[List[str]]
:type N: int
:rtype: int
"""
return sum(col.count('B') == 1 == picture[col.index('B')].count('B') \
for col in zip(*picture))

Python:

class Solution(object):
def findLonelyPixel(self, picture):
"""
:type picture: List[List[str]]
:rtype: int
"""
w, h = len(picture), len(picture[0])
rows, cols = [0] * w, [0] * h
for x in range(w):
for y in range(h):
if picture[x][y] == 'B':
rows[x] += 1
cols[y] += 1
ans = 0
for x in range(w):
for y in range(h):
if picture[x][y] == 'B':
if rows[x] == 1:
if cols[y] == 1:
ans += 1
return ans  

Python:

class Solution(object):
def findLonelyPixel(self, picture):
""" :type picture: List[List[str]] :rtype: int """
row = self.find_row(picture)
column =self.find_colum(picture) result = 0
for x in row:
for y in column:
if picture[x][y] == "B":
result += 1
return result def find_row(self, picture):
result = []
for x in xrange(len(picture)):
num = 0
for y in xrange(len(picture[x])):
if picture[x][y] == "B":
num += 1
if num == 1:
result.append(x)
return result def find_colum(self, picture):
result = []
for y in xrange(len(picture[0])):
num = 0
for x in xrange(len(picture)):
if picture[x][y] == "B":
num += 1
if num == 1:
result.append(y)
return result  

C++:

class Solution {
public:
int findLonelyPixel(vector<vector<char>>& picture) {
vector<int> rows = vector<int>(picture.size());
vector<int> cols = vector<int>(picture[0].size()); for (int i = 0; i < picture.size(); ++i) {
for (int j = 0; j < picture[0].size(); ++j) {
rows[i] += picture[i][j] == 'B';
cols[j] += picture[i][j] == 'B';
}
} int result = 0;
for (int i = 0; i < picture.size(); ++i) {
if (rows[i] == 1) {
for (int j = 0; j < picture[0].size() && rows[i] > 0; ++j) {
result += picture[i][j] == 'B' && cols[j] == 1;
}
}
}
return result;
}
};

C++:

class Solution {
public:
int findLonelyPixel(vector<vector<char>>& picture) {
if (picture.empty() || picture[0].empty()) return 0;
int m = picture.size(), n = picture[0].size(), res = 0;
vector<int> rowCnt(m, 0), colCnt(n, 0);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (picture[i][j] == 'B') {
++rowCnt[i];
++colCnt[j];
}
}
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (picture[i][j] == 'B') {
if (rowCnt[i] == 1 && colCnt[j] == 1) {
++res;
}
}
}
}
return res;
}
};

  

  

类似题目:

[LeetCode] 533. Lonely Pixel II 孤独的像素 II  

 

All LeetCode Questions List 题目汇总

 

[LeetCode] 531. Lonely Pixel I 孤独的像素 I的更多相关文章

  1. [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 ...

  2. [LeetCode] Lonely Pixel II 孤独的像素之二

    Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...

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

    Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...

  4. LeetCode 531. Lonely Pixel I

    原题链接在这里:https://leetcode.com/problems/lonely-pixel-i/ 题目: Given a picture consisting of black and wh ...

  5. LeetCode 531. Longly Pixel I (孤独的像素之一) $

    Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...

  6. 531. Lonely Pixel I

    Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...

  7. LeetCode 533. Lonely Pixel II (孤独的像素之二) $

    Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...

  8. 像素 PIXEL 图片的基本单位 像素非常小 图片是成千上万的像素组成 显示/屏幕分辨率 (DPI 屏幕分辨率)

    像素 PIXEL 图片的基本单位 像素非常小 图片是成千上万的像素组成 显示/屏幕分辨率 (DPI 屏幕分辨率) 图像分辨率 (PPI) 1920*1080是像素点长度1920个像素点 X1080个像 ...

  9. 533. Lonely Pixel II

    Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...

随机推荐

  1. Mysql【第三课】

  2. fastjson异常(字符串集合转成字符串数组)

    我是在项目中,因为受到一个string类型的list集合,然后需要把这个字符串发送给前端,进行解析. 但是前端收到的是一个字符串,不能进行解析. 所以采用 ArrayUtils.clone(JSONO ...

  3. IIS 使用 web.config 实现从 http 自动301跳转到 https 的方法

    现在很多网站为了安全,开启了 SSL 连接,那么开启 SSL 连接之后,如何将对应的 http 访问自动跳转到 https 上呢?之前介绍了 IIS 用 web.config 做域名的301跳转的方法 ...

  4. java-we不在esclipse创建servlet之后改名不起作用的问题归纳

    有时候我们不满意类名而去改名,但是改过了之后却发现不能实现它本来该实现的功能了,这是为什么呢,原因就是在2.5里面创建了servlet之后就会在web.xml里生成关于这个servlet的配置,你只是 ...

  5. Gym-100648B: Hie with the Pie(状态DP)

    题意:外卖员开始在0号节点,有N个人点了外卖,(N<=10),现在告诉两两间距离,问怎么配送,使得每个人的外卖都送外,然后回到0号点的总时间最短,注意,同一个点可以多次经过. 思路:TSP问题( ...

  6. xml的运用

    <?xml version="1.0" encoding="utf-8"?><class> <student> <na ...

  7. 使用mybatis框架实现带条件查询-多条件(传入Map集合)

    我们发现我们可以通过传入javaBean的方式实现我们的需求,但是就两个条件,思考:现在就给他传入一个实体类,对系统性能的开销是不是有点大了. 现在改用传入Map集合的方式: 奥!对了,在创建map集 ...

  8. if语句的嵌套:从键盘输入3个实数,求其最大值。

    #include<stdio.h>void main(){ float a,b,c,max; scanf("%f%f%f",&a,&b,&c); ...

  9. AJax的三种响应

    AJax的响应 1.普通文本方式(字符串) resp.getWriter().print("你好"); 2.JSON格式当要给前台页面传输 集合或者对象时 使用普通文本传输的时St ...

  10. 将idea中xml文件背景颜色去除(转)

    原贴链接:https://blog.csdn.net/weixin_43215250/article/details/89403678 第一步:除去SQL代码块的背景颜色,步骤如下 设置后还是很影响视 ...