昨晚,在做leetcode上的3Sum题目时,感觉这道题目和2Sum很像,当时解决2Sum时,思路如下:

用HashMap的key存储 num[i],value存储下标 i,之后在遍历数组num时,判断target-num[i]是否在HashMap的key中,大致解题思路是这样的。于是我决定继续用这个思路解决3Sum问题。思路如下:

用2层for循环(同理4Sum问题用3层for循环),Java代码(此段代码Time Limit Exceeded)如下:

public List<List<Integer>> threeSum(int[] num) {

        Arrays.sort(num);
List<List<Integer>> listAll = new ArrayList<List<Integer>>();
Map<Integer, Integer> hm = new HashMap<Integer, Integer>();
for(int i = 0; i < num.length; i++) {
hm.put(num[i], i);
}
for (int i = 0; i < num.length; i++) {
int oneTmp = -num[i];
if (oneTmp < 0) {
break;
}
for (int j = i + 1; j < num.length; j++) {
int thirdTmp = oneTmp - num[j];
if (hm.containsKey(thirdTmp) && hm.get(thirdTmp) > j) {
List<Integer> listTmp = new ArrayList<Integer>();
listTmp.add(num[i]);
listTmp.add(num[j]);
listTmp.add(thirdTmp);
if (!listAll.contains(listTmp)) {
listAll.add(listTmp);
}
}
}
}
return listAll;
}

上面的这段代码提交时,一直显示Time Limit Exceeded。经过检查,发现问题出现在上面的红色代码处,我是计算了很多重复的triplet后,然后再用listAll的contains判断并去重,这样的操作会占用一定的时间,导致Time Limit Exceeded。

第一次优化:优化第二层for循环的下标j的遍历方法,Java代码(很奇怪的是这段代码第一次提交时Accepted了,之后几次提交都是Time Limit Exceeded)如下:

public List<List<Integer>> threeSum(int[] num) {

        Arrays.sort(num);
List<List<Integer>> listAll = new ArrayList<List<Integer>>();
Map<Integer, Integer> hm = new HashMap<Integer, Integer>();
for(int i = 0; i < num.length; i++) {
hm.put(num[i], i);
}
for (int i = 0; i < num.length; i++) {
int oneTmp = -num[i];
if (oneTmp < 0) {
break;
}
for (int j = i + 1; j < num.length; j++) {
int thirdTmp = oneTmp - num[j];
if (hm.containsKey(thirdTmp) && hm.get(thirdTmp) > j) {
List<Integer> listTmp = new ArrayList<Integer>();
listTmp.add(num[i]);
listTmp.add(num[j]);
listTmp.add(thirdTmp); listAll.add(listTmp);
while ((j < num.length - 1) && (num[j] == num[j + 1])) {
j ++;
}
}
}
}
return listAll;
}

其实就是把第一段代码中的红色部分,替换成黄色部分。

第二次优化,就是优化第一层for循环的下标 i 的遍历方法,最终Java代码(这次以318ms Accepted了)如下:

public List<List<Integer>> threeSum(int[] num) {

        Arrays.sort(num);
List<List<Integer>> listAll = new ArrayList<List<Integer>>();
Map<Integer, Integer> hm = new HashMap<Integer, Integer>();
for(int i = 0; i < num.length; i++) {
hm.put(num[i], i);
}
for (int i = 0; i < num.length; i++) {
int oneTmp = -num[i];
if (oneTmp < 0) {
break;
}
for (int j = i + 1; j < num.length; j++) {
int thirdTmp = oneTmp - num[j];
if (hm.containsKey(thirdTmp) && hm.get(thirdTmp) > j) {
List<Integer> listTmp = new ArrayList<Integer>();
listTmp.add(num[i]);
listTmp.add(num[j]);
listTmp.add(thirdTmp);
listAll.add(listTmp);
while ((j < num.length - 1) && (num[j] == num[j + 1])) {
j ++;
}
}
}
while ((i < num.length - 1) && (num[i] == num[i + 1])) {
i ++;
}
}
return listAll;
}

其实就是在第二段代码的基础上,再加上面绿色代码。至此,基于HashMap的解决3Sum问题的Java代码优化结束~

3Sum Time Limit Exceeded HashMap 优化过程的更多相关文章

  1. ORA-19815,ORA-19809 :limit exceeded for recovery files

    数据库重新启动的时候,收到了ORA-19815的错误.从错误的提示来看,是由于闪回区的空间被填满导致无法成功启动.这种情形我们通常考虑的是清除归档日志,那就直接在OS层面rm了,真的是这样吗?客官,如 ...

  2. java.lang.OutOfMemoryError:GC overhead limit exceeded

    在调测程序时报java.lang.OutOfMemoryError:GC overhead limit exceeded 错误 错误原因:在用程序进行数据切割时报了该错误.由于在本地执行数据切割测试的 ...

  3. audit:backlog limit exceeded

    今天发现存储服务器业务不可用,服务器能ping通,远程不了!  到机房管理员那里查看服务器状态后,发现显示如下: 显然系统已经崩溃,只能先重启服务器,先恢复业务,然后针对backlog limit e ...

  4. unable to execute dex:GC overhead limit exceeded unable to execute dex:java heap space 解决方案

    最近做厂商适配,厂商提供了一部分Framework的jar包,把jar包通过Add Jar放到Build Path中, 在生成APK过程中,Eclipse长时间停留在100%那个进度. 最后Eclip ...

  5. Spark OOM:java heap space,OOM:GC overhead limit exceeded解决方法

    问题描述: 在使用spark过程中,有时会因为数据增大,而出现下面两种错误: java.lang.OutOfMemoryError: Java heap space java.lang.OutOfMe ...

  6. Spark 1.4.1中Beeline使用的gc overhead limit exceeded

    最近使用SparkSQL做数据的打平操作,就是把多个表的数据经过关联操作导入到一个表中,这样数据查询的过程中就不需要在多个表中查询了,在数据量大的情况下,这样大大提高了查询效率.   我启动了thri ...

  7. java.lang.OutOfMemoryError GC overhead limit exceeded原因分析及解决方案

    最近一个上线运行良好的项目出现用户无法登录或者执行某个操作时,有卡顿现象.查看了日志,出现了大量的java.lang.OutOfMemoryError: GC overhead limit excee ...

  8. [SpringBoot/SpringMVC]从Webapp下载一个大文件出现java.lang.OutOfMemoryError: GC overhead limit exceeded怎么办?

    本文示例工程下载:https://files.cnblogs.com/files/xiandedanteng/WebFileDownload20191026.rar 制作一个Webapp,让其中一个网 ...

  9. 转载:OutOfMemoryError系列(2): GC overhead limit exceeded

    这是本系列的第二篇文章, 相关文章列表: OutOfMemoryError系列(1): Java heap space OutOfMemoryError系列(2): GC overhead limit ...

随机推荐

  1. NOIP模拟赛18 皇帝的烦恼O(∩_∩)O 二分+DP

    题目描述 经过多年的杀戮,秦皇终于统一了中国.为了抵御外来的侵略,他准备在国土边境安置n名将军.不幸的是这n名将军羽翼渐丰,开始展露他们的狼子野心了.他们拒绝述职.拒绝接受皇帝的圣旨. 秦皇已经准备好 ...

  2. 修改 Django Administration

    只需要在django项目下的APP下的admin.py重写以下几个变量即可,不需要改django源码 from django.contrib import adminadmin.site.site_t ...

  3. 不止面试—jvm类加载面试题详解

    面试题 带着问题学习是最高效的,本次我们将尝试回答以下问题: 什么是类的加载? 哪些情况会触发类的加载? 讲一下JVM加载一个类的过程 什么时候会为变量分配内存? JVM的类加载机制是什么? 双亲委派 ...

  4. thinkphp 比RBAC更好的权限认证方式(Auth类认证)

    Auth 类已经在ThinkPHP代码仓库中存在很久了,但是因为一直没有出过它的教程, 很少人知道它, 它其实比RBAC更方便 . RBAC是按节点进行认证的,如果要控制比节点更细的权限就有点困难了, ...

  5. pip install xxx Could not fetch URL https://pypi.org/simple/pip/

    Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirmingthe ssl certificate: ...

  6. nyoj 23-取石子(一)(博弈)

    23-取石子(一) 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:20 submit:33 题目描述: 一天,TT在寝室闲着无聊,和同寝的人玩起了取 ...

  7. Flsk&pyecharts 动态数据可视化

    1:数据源 Hollywood Movie Dataset: 好莱坞2006-2011数据集  实验目的: 实现 统计2006-2011的数据综合统计情况,进行数据可视化 gitee地址: https ...

  8. linux网络测试命令

    一.ping 它通过向目标主机发送一个个数据包以及接受数据包的回应来判断主机和目标主机之间网络连接情况.ping的两个功能:判断网络是否可达.网络性能统计. ping使用的是网络层的ICMP协议. p ...

  9. EFK教程(3) - ElasticSearch冷热数据分离

    基于ElasticSearch多实例架构,实现资源合理分配.冷热数据分离 作者:"发颠的小狼",欢迎转载与投稿 目录 ▪ 用途 ▪ 架构 ▪ 192.168.1.51 elasti ...

  10. 小白学 Python 爬虫(4):前置准备(三)Docker基础入门

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...