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:

  1. 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----两种算法之间性能的比较的更多相关文章

  1. [LeetCode] 531. Lonely Pixel I 孤独的像素 I

    Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...

  2. LeetCode 531. Lonely Pixel I

    原题链接在这里:https://leetcode.com/problems/lonely-pixel-i/ 题目: Given a picture consisting of black and wh ...

  3. [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 ...

  4. 图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS)

    参考网址:图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS) - 51CTO.COM 深度优先遍历(Depth First Search, 简称 DFS) 与广度优先遍历(Breath ...

  5. 最小生成树算法 prim kruskal两种算法实现 HDU-1863 畅通工程

    最小生成树 通俗解释:一个连通图,可将这个连通图删减任意条边,仍然保持连通图的状态并且所有边权值加起来的总和使其达到最小.这就是最小生成树 可以参考下图,便于理解 原来的图: 最小生成树(蓝色线): ...

  6. KingbaseES 两表关联Update的两种写法与性能

    熟悉oracle 的人都知道,对于两表的关联更新,其执行计划主要有 Filter 和 Outer Join 两种方式.对于大批量数据的update,Join方式明显是更优的选择.KingbaseES ...

  7. LeetCode 531. Longly Pixel I (孤独的像素之一) $

    Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...

  8. 「每日五分钟,玩转JVM」:两种算法

    前言 上篇文章,我们了解了GC 的相关概念,这篇文章我们通过两个算法来了解如何去确定堆中的对象实例哪些是我们需要去回收的垃圾对象. 引用计数算法 引用计数法的原理很简单,就是在对象中维护一个计数器,当 ...

  9. 531. Lonely Pixel I

    Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...

随机推荐

  1. springcloud eureka注册中心 高可复用。

    1:新建两个注册中心项目(名称都为:spring-cloud-eureka,只是端口分别为8000.8001 ).两个注册中心相互注册对方. 2:两个注册中心都启动后,则对方服务列表都有对方的服务. ...

  2. Mac系统升级至OS X Mavericks后Genymotion出现的问题及解决方法

    Apple的系统升级终于免费了,可开心满满地升级到OS X Mavericks后,Android模拟器之王Genymotion罢工了.遇到两个问题:1. Unable to load VirtualB ...

  3. Mac下使用tree命令

    Mac下没有tree命令,但是可以通过brew进行安装,命令如下: brew install tree 装好后tree的用法和linux下的保持一致.参考:http://www.cnblogs.com ...

  4. springboot项目:登录 登录aop拦截 使用Redis与cookie 进行设置获取清除操作

    登录.登出: 第一步:在pom文件中引入依赖 <dependency> <groupId>org.springframework.boot</groupId> &l ...

  5. VS2015 release模式下进行debug调试

    有时候软件发布,又不得不调试其中的某个dll模块, 这时候就需要在发布的release版本的软件中来调试其中的dll模块了. vs2015设置: 1.Release模式下右键工作属性,选择C/C++, ...

  6. 关联容器:unordered_map详细介绍(附可运行代码)

    介绍 1 特性 2 Hashtable和bucket 模版 1 迭代器 功能函数 1 构造函数 12示例代码 2 容量操作 21 size 22 empty 3 元素操作 31 find 32 ins ...

  7. 十分有趣的this指向题

    var num=5;//9 =>++this.num=>10 var obj={ num:3, fn:function() { num = 9;//window.num=9 (functi ...

  8. 在进行make之前,configure的时候,请先清理config.cache

    在进行make之前,configure的时候,请先清理config.cache

  9. @Scheduled执行定时任务与cron表达式

    1 配置文件形式执行定时任务 1 1.X 版本与spring结合使用实例 1.1 常用maven管理 pom.xml文件 <project xmlns="http://maven.ap ...

  10. Varint数值压缩算法

    Varint 是一种紧凑的表示数字的方法.它用一个或多个字节来表示一个数字,值越小的数字使用越少的字节数.这能减少用来表示数字的字节数.比如对于 int32 类型的数字,一般需要 4 个 byte 来 ...