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. 微信小程序~跳页传参

    [1]需求: 点击商品,跳到相应商品详情页面 [2]代码: (1)商品列表页 <view class="goodsList"> <view wx:for=&quo ...

  2. NOIP2018 旅行 和 赛道修建

    填很久以前的坑. 旅行 给一棵 n 个点的基环树,求字典序最小的DFS序. n ≤ 5000 题解 O(n2) 做法非常显然,枚举断掉环上哪条边然后贪心即可.当然我去年的骚操作只能得88分. O(n ...

  3. GSM/GPRS模块 AT指令集C语言编程——基于有方M660+和MSP430单片机

    GSM/GPRS芯片是手机中负责收发短信.拨打电话以及访问GPRS网络的核心器件.有方M660+为深圳有方公司生产的一款超小封装的GSM/GPRS工业无线模块,可以提供高品质的语音.短信.数据业务等功 ...

  4. sql语句的面试题

    中科软的面试题:http://www.mayiwenku.com/p-5888480.html 中科软的面试题:https://blog.csdn.net/woolceo/article/detail ...

  5. Dubbo源码分析:Server

    Server接口是开启一个socket服务.服务实现有netty,mina,grizzly的. 抽象时序图 获取NettyServer时序图 Transporter类图 Server 类图

  6. hdu3625

    hdu3625 题意: 酒店发生一起谋杀案.作为镇上最好的侦探,您应该立即检查酒店的所有N个房间.但是,房间的所有门都是锁着的,钥匙刚锁在房间里,真是个陷阱!您知道每个房间里只有一把钥匙,并且所有可能 ...

  7. Codeforces Round #605 (Div. 3) E. Nearest Opposite Parity(最短路)

    链接: https://codeforces.com/contest/1272/problem/E 题意: You are given an array a consisting of n integ ...

  8. 含-SH的ACE抑制药的青霉胺样反应

    关于 含-SH的血管紧张素转化酶(ACE)抑制药如卡托普利具有青霉胺样反应.而依那普利则不含-SH. 青霉胺样反应 青霉胺样反应,指应用含-SH的ACE抑制药产生的皮疹.嗜酸性粒细胞(E)增多.味觉异 ...

  9. mysql 存储过程的定义和使用

    1)创建存储过程:并循环插入数据 create PROCEDURE sp_name() BEGIN DECLARE i int; ; DO ,); ; end while; END

  10. P5110 【块速递推】

    太菜了,不会生成函数,于是用特征方程来写的这道题 首先我们知道,形如\(a_n=A*a_{n-1}+B*a_{n-2}\)的特征方程为\(x^2=A*x+B\) 于是此题的递推式就是:\(x^2=23 ...