[Leetcode] 531. Lonely Pixel I 解题报告
来源:互联网 发布:java list方法 编辑:程序博客网 时间:2024/06/05 08:04
题目:
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: 3Explanation: 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,那么它就是孤立黑点了。
代码:
class Solution {public: int findLonelyPixel(vector<vector<char>>& picture) { 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); vector<pair<int, int>> black_positions; for (int r = 0; r < picture.size(); ++r) { for (int c = 0; c < picture[0].size(); ++c) { if (picture[r][c] == 'B') { black_positions.push_back(make_pair(r, c)); ++row_pixels[r], ++col_pixels[c]; } } } int ret = 0; for (int i = 0; i < black_positions.size(); ++i) { int r = black_positions[i].first, c = black_positions[i].second; if (row_pixels[r] == 1 && col_pixels[c] == 1) { ++ret; } } return ret; }};
阅读全文
0 0
- [Leetcode] 531. Lonely Pixel I 解题报告
- 531. Lonely Pixel I ----Leetcode
- [LeetCode]531. Lonely Pixel I
- [Leetcode] 533. Lonely Pixel II 解题报告
- 531. Lonely Pixel I
- Leetcode 533 Lonely Pixel II
- 532. K-diff Pairs in an Array\531. Lonely Pixel I
- 533. Lonely Pixel II
- 【LeetCode】Combination Sum I & II 解题报告
- 【LeetCode】House Robber I & II 解题报告
- 【LeetCode】Student Attendance Record I 解题报告
- 【LeetCode】Array Partition I 解题报告
- 【LeetCode】561.Array Partition I解题报告
- [Leetcode] 464. Can I Win 解题报告
- [Leetcode] 561. Array Partition I 解题报告
- leetCode解题报告之Palindrome Partitioning I,II(DFS,DP)
- leetCode解题报告之Palindrome Partitioning I,II(DFS,DP)
- leetCode解题报告之Palindrome Partitioning I,II(DFS,DP)
- coursera-斯坦福-机器学习-吴恩达-第8周笔记-无监督学习
- 迭代器模式
- Android 7.1 GUI系统-窗口管理WMS-动画的执行(七)
- 重识java7
- java 集合
- [Leetcode] 531. Lonely Pixel I 解题报告
- A Little Java, A fewer Patterns
- CMake入门
- bootstrap CSS概述
- MacBook Pro 链接无线Wi-Fi网络问题(Wi-Fi正常打开,无法连接路由器)
- 吴恩达深度学习笔记之卷积神经网络(目标检测)
- visual studio编写C#代码时“未能从程序集.....中加载类型”和“找不到方法”的一种可能的解决办法
- 如何通过git将项目推送到github远程仓库
- 【Linux】查看ubuntu版本与位数