昨晚,在做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. win+L键失灵了怎么办?

    win+L组合键是比较常用的锁屏快捷键组合,一直用的好好的今天发现突然失灵. 百度大部分方法是改注册表的值,然而对我来说没有用. 最后,才搜到一个帖子说是 win键被锁住了. [解决方法]: Fn+w ...

  2. Python基础学习(一)之Python的概述与环境安装

    Python介绍 Python语言介绍 Python是一门高级的.面向对象的.解释性.脚本语言. 高级语言:贴近开发者,对应底层语言,底层语言贴近机器:java.C#.php .ruby 面向对象对应 ...

  3. Linux基础命令(二)

    6.cp copy 作用:复制文件 选项: -a 复制目录时使用并且可以保持属性不变,属性:属主,属组,权限 -r 复制目录时使用但是不可以保持属性不变 -p 保持属性不变 注意:其实只需要记一个-a ...

  4. Ansibile之playbook初识

    一.playbook简介 ansiblie的任务配置文件被称为playbook,俗称“剧本”,每一个剧本(playbook)中都包含了一系列的任务,这每个任务在ansible中又被称为“戏剧”(pla ...

  5. Spring源码解析之@Configuration

    @Configuration简介 用于标识一个类为配置类,与xml配置效果类似 用法简介 public class TestApplication { public static void main( ...

  6. ASP.NET Core 3.0 gRPC 身份认证和授权

    一.开头聊骚 本文算是对于 ASP.NET Core 3.0 gRPC 研究性学习的最后一篇了,以后在实际使用中,可能会发一些经验之文.本文主要讲 ASP.NET Core 本身的认证授权和gRPC接 ...

  7. ubuntu18+uwsgi+nginx部署django项目

    更新系统软件源 sudo apt-get update pip3安装 sudo apt install python3-pip 安装virtualenvwrapper pip3 install vir ...

  8. C语言1博客作业06

    这个作业属于哪个课程 C语言程序设计II 这个作业的要求在哪里 https://www.cnblogs.com/sanying/p/11771502.html 我在这个课程的目标是 端正态度,认真对待 ...

  9. 目录(cd mkdir rmdir rm pwd ls) 文件(ln touch mv rm cat more head rail) 文件权限(chmod chown chgrp) 文件通配符(* ? [])

    记住Linux目录树的结构是一个称职Linux系统管理员的必备素质! 目录漫游cd   cd - 目录显示pwd 目录管理 mkdir -p a/b/c/1 parent创建多层目录 -m 700   ...

  10. 图解Elasticsearch的核心概念

    本文讲解大纲,分8个核心概念讲解说明: NRT Cluster Node Document&Field Index Type Shard Replica Near Realtime(NRT)近 ...