533. Lonely Pixel II
来源:互联网 发布:淘宝关键词数量 编辑:程序博客网 时间:2024/06/07 06:43
Given a picture consisting of black and white pixels, and a positive integer N, find the number of black pixels located at some specific row Rand column C that align with all the following rules:
- Row R and column C both contain exactly N black pixels.
- For all rows that have a black pixel at column C, they should be exactly the same as row R
The picture is represented by a 2D char array consisting of 'B' and 'W', which means black and white pixels respectively.
Example:
Input: [['W', 'B', 'W', 'B', 'B', 'W'], ['W', 'B', 'W', 'B', 'B', 'W'], ['W', 'B', 'W', 'B', 'B', 'W'], ['W', 'W', 'B', 'W', 'B', 'W']] N = 3Output: 6Explanation: All the bold 'B' are the black pixels we need (all 'B's at column 1 and 3). 0 1 2 3 4 5 column index 0 [['W', 'B', 'W', 'B', 'B', 'W'], 1 ['W', 'B', 'W', 'B', 'B', 'W'], 2 ['W', 'B', 'W', 'B', 'B', 'W'], 3 ['W', 'W', 'B', 'W', 'B', 'W']] row indexTake 'B' at row R = 0 and column C = 1 as an example:Rule 1, row R = 0 and column C = 1 both have exactly N = 3 black pixels. Rule 2, the rows have black pixel at column C = 1 are row 0, row 1 and row 2. They are exactly the same as row R = 0.
Note:
- The range of width and height of the input 2D array is [1,200].
public class Solution { public int findBlackPixel(char[][] picture, int N) { if (picture == null || picture.length == 0) return 0; int m = picture.length, n = picture[0].length; int[] rowCount = new int[m]; int[] colCount = new int[n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (picture[i][j] == 'B') { rowCount[i]++; colCount[j]++; } } } int count = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (picture[i][j] == 'B' && rowCount[i] == N && colCount[j] == N) { String iStr = String.valueOf(picture[i]); int kCount = 0; for (int k = 0; k < m; k++) { if (i != k && picture[k][j] == 'B') { String kStr = String.valueOf(picture[k]); if (!iStr.equals(kStr)) break; else kCount++; } } if (kCount == N-1) count++; } } } return count; }}
阅读全文
0 0
- 533. Lonely Pixel II
- [Leetcode] 533. Lonely Pixel II 解题报告
- Leetcode 533 Lonely Pixel II
- 531. Lonely Pixel I
- 531. Lonely Pixel I ----Leetcode
- [LeetCode]531. Lonely Pixel I
- [Leetcode] 531. Lonely Pixel I 解题报告
- lonely
- lonely
- Lonely lonely lonely...
- 532. K-diff Pairs in an Array\531. Lonely Pixel I
- pixel
- Pixel
- [Music]Lonely
- Lonely Christmas!
- 恩,lonely
- Lonely Low
- Mr.lonely
- LINUX 文件系统
- SSH框架详解
- Android 富文本编辑器,支持图文混排,支持插入和删除图片
- 数据库日期格式为datetime时,存储日期的方法
- mysql---插入数据
- 533. Lonely Pixel II
- 【EasyUI】EasyUI中分页和筛选功能的冲突解决
- 在 Eclipse 中编写的 Android 项目的包名修改
- SQL
- UVALive2356 ZOJ1061 Web Navigation【堆栈+模拟】
- Spring Cloud构建微服务架构(四)分布式配置中心
- [Unity&GameSparks]建立云代码,以及测试
- 快递100接口总结
- FFMPEG学习【libavformat】:Demuxing