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们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- MATBLAB学习笔记----基础绘图
整理自台大生机系郭彦甫.MATLAB系列教程,吐血推荐看这个视频,非计算机专业也能看懂,全程干货 MATLAB图形来自于“数据”,MATLAB不能理解函数. MATLAB绘图原理: 1.在特定范围生成 ...
- Codeforces Round #557 (Div. 1) 简要题解
Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...
- 什么是SQL ?
SQL 1.什么是SQL ? Structured Query Languange:结构化查询语言 其实就是定义了操作所有关系型数据库的规则.每一种数据库操作的方式存在不一样的地方,称为“方言”. 2 ...
- NVDLA软件架构和源码解析 第一章—内核驱动【华为云技术分享】
驱动整体设计介绍 不同的processor Nvidia DLA的内核驱动KMD(Kernel mode driver)中,并不是把DLA当成一个设备来控制,而是把不同的功能模块当做不同的proces ...
- 开发dubbo应用程序(二)dubbo注册中心相关概述
1.注册中心概述 在Dubbo微服务体系中,注册中心是其核心组件之一.Dubbo通过注册中心实现了分布式环境中各微服务之间的注册与发现,是各分布式节点之间的纽带.其主要作用如下: 动态加入.一个服 ...
- How to signout from an Azure Application?(转载)
问: I have created a Azure AD application and a Web App. The Azure AD Application uses AAD Authentica ...
- 八.软件自动化和web测试
1.软件自动化测试 1.1 自动化测试的概念 自动化测试:就是通过测试工具或其他手段,按照测试工程师的预定计划对软件产品进行自动化的测试 软件测试自动化涉及到测试流程.测试体系.自动化编译以 ...
- python递归函数的执行过程
举例: def nove(n,a,b,c): if n == 1: print(a,'------------>',c) else: nove(n-1,a,c,b) nove(1,a,b,c) ...
- Java开发环境配置大全
Java开发环境配置 零章:JDK安装教程 壹章:Tomcat安装教程 贰章:IntelliJ IDEA安装教程 叁章:MySql安装教程 肆章:Maven安装教程 伍章:MongoDB安装教程 陆章 ...
- 【Axure】原型设计工具的概览与初识
软件工程综合实践第三次个人作业 作业要求:通过搜索资料和自学,了解原型设计的工具 前言: Axure是一款强大的原型设计工具,具有比较大的用户基础,在此前提下沟通.传输.修改就显得十分方便,并且在细节 ...