LeetCode 531. Longly 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].
题目标签:Array
题目给了我们一个2d picture array,让我们找出有几个孤独的像素B。孤独的像素B的行和列中只有自己一个像素。
可以建立2个 array, 分别是 row 和 col, 它们的size 就是picture里的行和列的size。
第一次遍历picture:如果是B,就把B的row 和 column 在 row array 和 col array 里对应位置 加1。目的是为了记录每一行和每一列里有多少个B。
第二次遍历picture:如果是B,就到对应的 row 和 col array 里, 如果 row 和 col 的值都是1 的话,说明这一个B 是 row 这一行里, 和 col 这一列里唯一的B。累计计数到res。
Java Solution:
Runtime beats 80.41%
完成日期:09/24/2017
关键词:Array
关键点:设立row array & col array 记录每一行每一列里有多少个B;孤单像素所在的行和列只有它自己
class Solution
{
public int findLonelyPixel(char[][] picture)
{
int n = picture.length;
int m = picture[0].length;
// create two array for counting B
int[] row = new int[n];
int[] col = new int[m]; int res = 0; // counts longly black // 1st iteration, if find a B, increase its row and col number in two array
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(picture[i][j] == 'B')
{
row[i]++;
col[j]++;
}
}
} // 2nd iteration, if find a B, check its row and col are both 1, meaning lonly B
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(picture[i][j] == 'B' && row[i] == 1 && col[j] == 1)
res++; return res;
}
}
参考资料:
https://discuss.leetcode.com/topic/81680/java-o-nm-time-with-o-n-m-space-and-o-1-space-solutions
LeetCode 题目列表 - LeetCode Questions List
LeetCode 531. Longly 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] 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 ...
- LeetCode 531. Lonely Pixel I
原题链接在这里:https://leetcode.com/problems/lonely-pixel-i/ 题目: Given a picture consisting of black and wh ...
- 像素 PIXEL 图片的基本单位 像素非常小 图片是成千上万的像素组成 显示/屏幕分辨率 (DPI 屏幕分辨率)
像素 PIXEL 图片的基本单位 像素非常小 图片是成千上万的像素组成 显示/屏幕分辨率 (DPI 屏幕分辨率) 图像分辨率 (PPI) 1920*1080是像素点长度1920个像素点 X1080个像 ...
- LeetCode 533. Lonely Pixel II (孤独的像素之二) $
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- 531. Lonely Pixel I
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- Efficient and Accurate Arbitrary-Shaped Text Detection with Pixel Aggregation Network(利用像素聚合网络进行高效准确的任意形状文本检测)
PSENet V2昨日刚出,今天翻译学习一下. 场景文本检测是场景文本阅读系统的重要一步,随着卷积神经网络的快速发展,场景文字检测也取得了巨大的进步.尽管如此,仍存在两个主要挑战,它们阻碍文字检测部署 ...
随机推荐
- informix服务端口和oralce服务端口
查找informix的服务端口1>>more .profile 找到: INFORMIXDIR=/home/informix INFORMIXSERVER=aaaa2>>cd ...
- python之socket编程------粘包
一.粘包 什么是粘包 只有TCP只有粘包现象,UDP永远不会粘包 所谓粘包问题主要还是因为接收方不知道之间的界限,不知道一次性提取多少字节的数据所造成的 两种情况发生粘包: 1.发送端需要等缓冲区满才 ...
- sed命令基础
sed是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(pattern space ...
- SSH框架搭建最终版【测试、log4j、baseDao】
最详细搭建SSH框架环境 本博文主要是讲解如何搭建一个比较规范的SSH开发环境,以及对它测试[在前面的搭建中,只是整合了SSH框架,能够使用SSH实现功能],而这次是相对规范的. 导入开发包 在Str ...
- oracle sql 树操作
语法:select-start with-connect by-prior 主要有两点 1)prior放在子节点端,则表示扫描树是以start with指定的节点作为根节点从上往下扫描.可能对应一个或 ...
- Spring MVC中Filter Servlet Interceptor 执行顺序
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springfr ...
- MVC发布网站
首先Vs打开解决方案 在Global.asax中加入下列代码,否则会出现CSS JS失效 BundleTable.EnableOptimizations = false; 用户 'NT AUTHORI ...
- 安装 node-sass 时报错
在安装 node-sass 时报错,截图如下 解决方法如下: npm install --save node-sass --registry=https://registry.npm.taobao.o ...
- LCM Cardinality 暴力
LCM Cardinality Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit St ...
- hdu4027 开方,记录
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...