[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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # 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:
1 2 3 4 5 6 7 8 9 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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 题目汇总
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架