[LeetCode] 531. Lonely Pixel I 孤独的像素 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:
- 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的更多相关文章
- [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 ...
- [LeetCode] Lonely Pixel II 孤独的像素之二
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- [LeetCode] Lonely Pixel I 孤独的像素之一
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- LeetCode 531. Lonely Pixel I
原题链接在这里:https://leetcode.com/problems/lonely-pixel-i/ 题目: Given a picture consisting of black and wh ...
- LeetCode 531. Longly Pixel I (孤独的像素之一) $
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- 531. Lonely Pixel I
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- LeetCode 533. Lonely Pixel II (孤独的像素之二) $
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- 像素 PIXEL 图片的基本单位 像素非常小 图片是成千上万的像素组成 显示/屏幕分辨率 (DPI 屏幕分辨率)
像素 PIXEL 图片的基本单位 像素非常小 图片是成千上万的像素组成 显示/屏幕分辨率 (DPI 屏幕分辨率) 图像分辨率 (PPI) 1920*1080是像素点长度1920个像素点 X1080个像 ...
- 533. Lonely Pixel II
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
随机推荐
- php的插入排序
感觉在这个数据量上,排入比冒泡要好很多呢~ 代码: <?php /** * 直接插入排序(类比抓牌) * 原理:每次从无序列表中取出第一个元素,把他插入到有序表中的合适位置,使有序表仍然有序 * ...
- 解决Invalid character found in the request target. The valid characters are defined in RFC 7230 and RF
通过这里的回答,我们可以知道: Tomcat在 7.0.73, 8.0.39, 8.5.7 版本后,添加了对于http头的验证. 具体来说,就是添加了些规则去限制HTTP头的规范性 参考这里 具体来说 ...
- 51nod1712 区间求和
http://www.51nod.com/Challenge/Problem.html#problemId=1712 先考虑题面中的简化问题. 对于\(i\in [1,n]\),\(a_i\)的贡献为 ...
- 使用Zookeeper实现负载均衡原理
思路 使用Zookeeper实现负载均衡原理,服务器端将启动的服务注册到,zk注册中心上,采用临时节点.客户端从zk节点上获取最新服务节点信息,本地使用负载均衡算法,随机分配服务器. 创建项目工程 M ...
- Python tkinter模块弹出窗口及传值回到主窗口操作详解
这篇文章主要介绍了Python tkinter模块弹出窗口及传值回到主窗口操作,结合实例形式分析了Python使用tkinter模块实现的弹出窗口及参数传递相关操作技巧,需要的朋友可以参考下 本文实例 ...
- Linux学习笔记——管道PIPE
管道:当从一个进程连接数据流到另一个进程时,使用术语管道(pipe).# include <unistd.h> int pipe(int filedes[2]); //创建管道 pipe( ...
- rs485一主多从的连接方式及通信注意事项
rs485的通信方式看似比较简单,其实通信软件的处理还是有需要注意的. 下图是主机向从机发送信息的示意图,其中485的线都是手牵手相连的,因此主机向下发的时候,其实各个从机都有在接收数据的,只是,从机 ...
- LeetCode 787. Cheapest Flights Within K Stops
原题链接在这里:https://leetcode.com/problems/cheapest-flights-within-k-stops/ 题目: There are n cities connec ...
- ctx控制超时的使用
cancel package main import ( "context" "fmt" "time" ) func gen(ctx con ...
- learning java FileWriter
import java.io.FileWriter; import java.io.IOException; public class FileWriterTest { public static v ...