LeetCode 531. Lonely Pixel I
原题链接在这里:https://leetcode.com/problems/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].
题解:
Have a row array and column array to track how many B on the corresponding row or column.
Iterate the picture for the 1st time and update row and column.
Iterate the prictrue for the 2nd time to accumlate the count when both r[i] and c[j] == 1.
Time Complexity: O(m*n). m = picture.length. n = picture[0].length.
Space: O(m+n).
AC Java:
class Solution {
public int findLonelyPixel(char[][] picture) {
if(picture == null || picture.length == 0 || picture[0].length == 0){
return 0;
}
int m = picture.length;
int n = picture[0].length;
int [] r = new int[m];
int [] c = new int[n];
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
if(picture[i][j] == 'B'){
r[i]++;
c[j]++;
}
}
}
int res = 0;
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
if(picture[i][j] == 'B' && r[i] == 1 && c[j] == 1){
res++;
}
}
}
return res;
}
}
LeetCode 531. Lonely Pixel I的更多相关文章
- [LeetCode] 531. Lonely Pixel I 孤独的像素 I
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- [LeetCode] 533. Lonely Pixel II 孤独的像素 II
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- LeetCode 531. Longly Pixel I (孤独的像素之一) $
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- 531. Lonely Pixel I
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- LeetCode 533. Lonely Pixel II (孤独的像素之二) $
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- [LeetCode] Lonely Pixel II 孤独的像素之二
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- [LeetCode] Lonely Pixel I 孤独的像素之一
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- 533. Lonely Pixel II
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- 1083 是否存在相等的差 PAT (Basic Level)
题目链接: https://pintia.cn/problem-sets/994805260223102976/problems/994805260780945408 分析: 将某个差值的次数存在数组 ...
- LOJ2874 JOISC2014 历史研究 分块、莫队
传送门 看到出现次数自然地考虑莫队. 但是发现如果需要删除并动态维护答案的话,则要用一个堆来维护答案,增加了一个\(log\).但是加入操作却没有这个\(log\),所以我们考虑避免删除操作. 分块, ...
- SQL系列(二)—— 查询(select)
在开始之前先了解下SQL中的操作分类.根据与数据库不同操作的交互,对数据不同的处理类型,可以将SQL分为四种:插入.删除.修改.查询.本篇文章中主要介绍查询操作.其实查询操作也是日常应用使用最为频繁且 ...
- What Is HLS (HTTP Live Streaming)?
HTTP Live Streaming (HLS) Executive Summary HTTP Live Streaming (or HLS) is an adaptive streaming c ...
- C#读写修改设置调整UVC摄像头画面-色调
有时,我们需要在C#代码中对摄像头的色调进行读和写,并立即生效.如何实现呢? 建立基于SharpCamera的项目 首先,请根据之前的一篇博文 点击这里 中的说明,建立基于SharpCamera的摄像 ...
- 处理收到的Stanzas
处理收到的Stanzas 背部 Smack使用两种结构提供了一个灵活的框架来处理传入的节: org.jivesoftware.smack.StanzaCollector - 一个允许您同步等待新节的类 ...
- Java IO---缓冲流和转换流
一. 缓冲流 缓冲流是处理流的一种,也叫高效流,是对4个基本输入输出流的增强,它让输入输出流具有1个缓冲区,能显著减小与外部的IO次数,从而提高读写的效率,并且提供了一些额外的读写方法. 因为 ...
- 2019 拉卡拉java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.拉卡拉等公司offer,岗位是Java后端开发,因为发展原因最终选择去了拉卡拉,入职一年时间了,也成为了面试官 ...
- python基础03--int,bool,str
1.1 数字int 1.i = 100 i.bit_length() 转化为二进制的最小位数 1.2 布尔 bool 1.True False 0是False 1.3 数据转换 ...
- Java自学-日期 日期格式化
Java中使用SimpleDateFormat 进行日期格式化类 SimpleDateFormat 日期格式化类 示例 1 : 日期转字符串 y 代表年 M 代表月 d 代表日 H 代表24进制的小时 ...