[Leetcode] 533. Lonely Pixel II 解题报告
来源:互联网 发布:联想手机数据恢复 编辑:程序博客网 时间:2024/06/05 17:45
题目:
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 R and 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].
思路:
和上一道题目类似,只不过加了一个额外条件,就是在某个位置上的黑点如果要符合条件,在该列上为黑点的所有行中的内容还需要相等。为此我们定义了一个哈希表和一个string构成的行向量,这是因为将vector<char>转化为string之后,将便于比较异同;另外用哈希表可以将行向量string映射成为其出现的次数。这样后面的比较就更加方便了。此时picture[r][c]如果满足如下几条则为符合条件的点:1)row_pixels[r] == N;2)col_pixels[c] == N;3)same_rows[rows[r]] == N。
代码:
class Solution {public: int findBlackPixel(vector<vector<char>>& picture, int N) { if (picture.size() == 0 || picture[0].size() == 0) { return 0; } vector<int> row_pixels(picture.size(), 0); vector<int> col_pixels(picture[0].size(), 0); unordered_map<string, int> same_rows; // map the row string to appearance count vector<string> rows; // convert the vector<char> to string for easier comparison for (int r = 0; r < picture.size(); ++r) { string s; for (int c = 0; c < picture[r].size(); ++c) { if (picture[r][c] == 'B') { ++row_pixels[r], ++col_pixels[c]; } s.push_back(picture[r][c]); } ++same_rows[s]; rows.push_back(s); } int ret = 0; for (int r = 0; r < picture.size(); ++r) { if (row_pixels[r] == N && same_rows[rows[r]] == N) { for (int c = 0; c < picture[r].size(); ++c) { if (picture[r][c] == 'B' && col_pixels[c] == N) { ++ret; } } } } return ret; }};
阅读全文
0 0
- [Leetcode] 533. Lonely Pixel II 解题报告
- [Leetcode] 531. Lonely Pixel I 解题报告
- 533. Lonely Pixel II
- Leetcode 533 Lonely Pixel II
- 531. Lonely Pixel I ----Leetcode
- [LeetCode]531. Lonely Pixel I
- 【LeetCode】Subsets II 解题报告
- 【LeetCode】Permutations II 解题报告
- [LeetCode] Permutations II 解题报告
- [LeetCode] Subsets II 解题报告
- Subset II [Leetcode 解题报告]
- Permutations II [Leetcode 解题报告]
- 531. Lonely Pixel I
- LeetCode — Word Break II 解题报告
- [LeetCode]Unique Paths II, 解题报告
- [LeetCode]Reverse Linked List II, 解题报告
- Leetcode Jump Game II 解题报告
- Leetcode Word Ladder II 解题报告
- Linux下 redmine安装部署
- Day016
- nodeJS学习笔记
- 【转】react-native-wechat--微信登录
- java 关键字
- [Leetcode] 533. Lonely Pixel II 解题报告
- Ceph对象存储网关中的索引工作原理
- 工厂模式(简单工厂+工厂方法+抽象工厂)
- Day017
- 《机器学习(周志华)》 西瓜数据集3.0(含规范化以后数据)
- iOS11新特性:新增拖拽交互体验
- CTK 插件之间的依赖
- SQLite3接口函数
- LINUX-01RED HAT ENTERPRISE LINUX 7(RHEL7)配置静态IP地址