LeetCode 531----Lonely Pixel I----两种算法之间性能的比较
Lonely Pixel I 两种算法之间的性能比较
今天参加LeetCode Weekly Contest 22,第二题 "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].
我的解法 (Java)
根据 picture 的行数和列数生成两个数组 rowcount 和 colcount,并声明一个 inrowindex 数组,维数和rowcount的维数相同,用来记录 rowcount[i] 中的 'B' 所在的列数 j ;然后遍历 picture ,如果当前(i,j)位置处的Pixel为 'B' ,则进行 rowcount[i]++ 和 colcount[j]++操作, 并进行 inrowindex[i]=j 操作。
上述步骤操作完毕后,考察 rowcount[i]1 的那些行,如果rowcount[i]1,且 colcount[inrowindex[i]]==1,那么说明 picture[i][inrowindex[i]] 处的 'B' 为 Lonely Pixel。
算法实现如下:
public class Solution {
public int findLonelyPixel(char[][] picture) {
int rows=picture.length;
int cols=picture[0].length;
int[] colcount=new int[cols];
int[] rowcount=new int[rows];
int[] inrowindex=new int[rows];
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
if(picture[i][j]=='B'){
rowcount[i]++;
inrowindex[i]=j;//记录rows[i]处的'B'出现在第几列
colcount[j]++;
}
}
}
int count=0;
for(int i=0;i<rows;i++){
if(rowcount[i]==1){
if(colcount[inrowindex[i]]==1){
count++;
}
}
}
return count;
}
}
但是提交算法后,显示的结果是一个大的二维数组运算超时! 实在找不到更好的算法了。
晚上在网上看到了有人分享的算法,是用Python写的:
Python 算法
class Solution(object):
def findLonelyPixel(self, picture):
"""
:type picture: List[List[str]]
:rtype: int
"""
w, h = len(picture), len(picture[0])
rows, cols = [0] * w, [0] * h
for x in range(w):
for y in range(h):
if picture[x][y] == 'B':
rows[x] += 1
cols[y] += 1
ans = 0
for x in range(w):
for y in range(h):
if picture[x][y] == 'B':
if rows[x] == 1:
if cols[y] == 1:
ans += 1
return ans
感觉这个算法的思路和我的类似,而该算法第二步统计 Lonely Pixel 的个数的时候,又进行了一次二维数组遍历,还不如我的算法高效。但试着提交该算法,却 Accepted 了!
难道是Python的测试样例跟Java的测试样例不同导致的?
于是将该 Python 算法改写成 Java 算法,如下:
public class Solution {
public int findLonelyPixel(char[][] picture) {
int rows=picture.length;
int cols=picture[0].length;
int[] colcount=new int[cols];
int[] rowcount=new int[rows];
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
if(picture[i][j]=='B'){
rowcount[i]++;
colcount[j]++;
}
}
}
int count=0;
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
if(picture[i][j]=='B'){
if(rowcount[i]==1&&colcount[j]==1){
count++;
}
}
}
}
return count;
}
}
竟然 Accepted了!
这是怎么回事呢?
第一个二层 for 循环,我仅仅多了一个 inrowindex[i]=j 操作,但这步操作正是为了第二个 for 循环只进行一层循环,为的是当 rowcount[i] 为 1 时,快速定位到这一行的 'B' 所在的列数,并考察这一列是否只有一个 'B'。 明明我的算法更高效呀!
这是怎么回事呢?
2017年3月5日20:00补充:
同样的算法,上午比赛的时候TLE,晚上又试了一次,这次竟然Accepted!! 什么鬼?
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 531. Lonely Pixel I
原题链接在这里:https://leetcode.com/problems/lonely-pixel-i/ 题目: Given a picture consisting of black and wh ...
- [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 ...
- 图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS)
参考网址:图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS) - 51CTO.COM 深度优先遍历(Depth First Search, 简称 DFS) 与广度优先遍历(Breath ...
- 最小生成树算法 prim kruskal两种算法实现 HDU-1863 畅通工程
最小生成树 通俗解释:一个连通图,可将这个连通图删减任意条边,仍然保持连通图的状态并且所有边权值加起来的总和使其达到最小.这就是最小生成树 可以参考下图,便于理解 原来的图: 最小生成树(蓝色线): ...
- KingbaseES 两表关联Update的两种写法与性能
熟悉oracle 的人都知道,对于两表的关联更新,其执行计划主要有 Filter 和 Outer Join 两种方式.对于大批量数据的update,Join方式明显是更优的选择.KingbaseES ...
- LeetCode 531. Longly Pixel I (孤独的像素之一) $
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- 「每日五分钟,玩转JVM」:两种算法
前言 上篇文章,我们了解了GC 的相关概念,这篇文章我们通过两个算法来了解如何去确定堆中的对象实例哪些是我们需要去回收的垃圾对象. 引用计数算法 引用计数法的原理很简单,就是在对象中维护一个计数器,当 ...
- 531. Lonely Pixel I
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
随机推荐
- [原创] Trie树 php 实现敏感词过滤
目录 背景 简介 存储结构 PHP 其他语言 字符串分割 示例代码 php 优化 缓存字典树 常驻服务 参考文章 背景 项目中需要过滤用户发送的聊天文本, 由于敏感词有将近2W条, 如果用 str_r ...
- 【APUE】第3章 文件I/O (1)
1.文件描述符 对于内核来说,所有打开的文件都通过文件描述符来引用.文件描述符是一个非负整数.当打开一个现有的文件或者创建一个新文件时,内核向进程返回一个文件描述符.当读.写一个文件时,使用open或 ...
- StarUML使用简明教程
最近了解到StarUML比较多,所以写一篇教程供大家参考,不足支持,请见谅. StarUML(简称SU),是一种创建UML类图,生成类图和其他类型的统一建模语言(UML)图表的工具.StarUML是一 ...
- [Xamarin]測試帳號申請與到期後如何續用 (转帖)
在Xamarin網站上可以申請30天試用的測試帳號.試用期內,Xamarin會提供完整的功能試用. 30天試用時間到期後,在Visual Studio裡面你載入你的專案的時候,專案旁會標註(無法使用) ...
- Docker MySQL基本操作
1 启动mysql实例 docker run --name some-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:t ...
- XXX is not in the sudoers file. This incident will be reported 的问题解决方案
不多说,直接上干货! 说的是,这种问题,是出现在ubuntu系统里. root@SparkSingleNode:/usr/local/jdk# pwd /usr/local/jdk root@Spar ...
- 使用WSW将Nginx创建为Windows系统服务
我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我们称之为“服务”,在Window ...
- Windows7 x64 了解堆
一.前言 堆对于开发者一般来说是熟悉又陌生的,熟悉是因为我们常常使用new/delete或者malloc/free使用堆,陌生是因为我们基本没有去了解堆的结构.堆在什么地方?怎么申请?怎么释放?系统又 ...
- R语言数据重塑cbind+rbind+merge+ melt+cast
R语言中的数据重塑是关于变化的数据分为行和列的方式.大多数R地数据处理的时候是通过将输入的数据作为一个数据帧进行.这是很容易提取一个数据帧的行和列数据,但在某些情况,当我们需要的数据帧的格式是不同的来 ...
- json跨域问题
一.跨域问题的原因: 1 浏览器的检查 2 跨域 3 XMLHttpRequest请求 二.跨域问题的解决: 1 禁止浏览器检查: 使用dos命令,在启动浏览器的时候,加一个参数: chrome -- ...