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昨日刚出,今天翻译学习一下. 场景文本检测是场景文本阅读系统的重要一步,随着卷积神经网络的快速发展,场景文字检测也取得了巨大的进步.尽管如此,仍存在两个主要挑战,它们阻碍文字检测部署 ...
随机推荐
- 设置文件opendilag、savedilog默认路径和文件类型
dlgSave1.Filter:='文件.txt|*.txt;|word文件|*.doc,*.docx'; dlgSave1.InitialDir:=GetCurrentDir; //设置为当前路径 ...
- python之线程相关的其他方法
一.join方法 (1)开一个主线程 from threading import Thread,currentThread import time def walk(): print('%s is r ...
- webservice第三篇【接口开发webservice、CXF框架使用、IDEA下使用webservice、小例子】
实现接口的webservice 服务端 import javax.jws.WebService; /**面向接口的webservice发布方式 * * */ @WebService public in ...
- java 对象转型
一.对象转型介绍 对象转型分为两种:一种叫向上转型(父类对象的引用或者叫基类对象的引用指向子类对象,这就是向上转型),另一种叫向下转型.转型的意思是:如把float类型转成int类型,把double类 ...
- 五年 Web 开发者 star 的 github 整理说明
欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ 作者:樊东东 前端从业几年,积累了不少github开源库. 有时候想查阅以前star的库,但不好找,github大多库都是英文说明,对中文 ...
- shell查找指定时间段内的文件
#!/bin/bash#20170905 输入参数格式echo "显示"$1"的备份文件"date_0=$1date_1=`expr $date_0 + 1`d ...
- 简单说明CGI是什么
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- Beauty Contest 凸包+旋转卡壳法
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 27507 Accepted: 8493 D ...
- 【bzoj2761】[JLOI2011]不重复数字
给出N个数,要求把其中重复的去掉,只保留第一次出现的数. 例如,给出的数为1 2 18 3 3 19 2 3 6 5 4,其中2和3有重复,去除后的结果为1 2 18 3 19 6 5 4. Inpu ...
- GBDT(MART)概念简介
GBDT(Gradient Boosting Decision Tree) 又叫 MART(Multiple Additive Regression Tree),是一种用于回归的机器学习算法,该算法由 ...