首页 > 代码库 > Lonely Pixel I
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:
- The range of width and height of the input 2D array is [1,500].
1 public class Solution { 2 public int findLonelyPixel(char[][] picture) { 3 if (picture == null || picture[0] == null) return 0; 4 5 int m = picture.length, n = picture[0].length; 6 boolean[] rows = new boolean[m]; 7 Arrays.fill(rows, true); 8 boolean[] cols = new boolean[n]; 9 Arrays.fill(cols, true); 10 11 for (int i = 0; i < m; i++) { 12 int rowCount = 0; 13 for (int j = 0; j < n; j++) { 14 if (picture[i][j] == ‘B‘) 15 rowCount++; 16 } 17 if (rowCount > 1) rows[i] = false; 18 } 19 20 for (int j = 0; j < n; j++) { 21 int colCount = 0; 22 for (int i = 0; i < m; i++) { 23 if (picture[i][j] == ‘B‘) 24 colCount++; 25 } 26 if (colCount > 1) cols[j] = false; 27 } 28 29 int result = 0; 30 for (int i = 0; i < m; i++) { 31 for (int j = 0; j < n; j++) { 32 if (picture[i][j] == ‘B‘ && rows[i] && cols[j]) 33 result++; 34 } 35 } 36 return result; 37 } 38 }
Lonely Pixel I
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。