昨晚,在做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. 接口自动化、移动端、web端自动化如何做?

    1.<Python+Appium移动端自动化项目实战>-带您进入APP自动化测试的世界https://yuedu.baidu.com/ebook/765b38a5690203d8ce2f0 ...

  2. 羞,Java 字符串拼接竟然有这么多姿势

    二哥,我今年大二,看你分享的<阿里巴巴 Java 开发手册>上有一段内容说:"循环体内,拼接字符串最好使用 StringBuilder 的 append 方法,而不是 + 号操作 ...

  3. SpringBoot 源码解析 (一)----- SpringBoot核心原理入门

    Spring Boot 概述 Build Anything with Spring Boot:Spring Boot is the starting point for building all Sp ...

  4. go xml 序列化

    /** 序列化 */ package main import ( "encoding/xml" "fmt" ) // Person 结构 type Person ...

  5. Python——标识符的命名规则

    01 Python语言的特点 python的语言特点有很多,我们这里只讲一点,python是一门面向对象的语言,即一切皆对象(Linux中有一句是:一切皆文件),括号内的只是打个比方,不懂也没事,就是 ...

  6. dubbo监控安装

    tar xf dubbo-monitor-simple-2.8.4-assembly.tar.gz cd dubbo-monitor-simple-2.8.4 vi conf/dubbo.proper ...

  7. 映客直播软开校招岗(go语言)

    问题: 笔试: 比较简单,有一道题比较深刻: 内存1G,需要计算1G的数据排序,哪种排序方法效率最低,当时选的是冒泡,因为涉及到频繁的数据交换,其实应该是归并,因为归并不是原地排序,多占用的内存空间, ...

  8. .NET Core 对龙芯的支持情况和对 .NET Core 开发嵌入式的思考

    目录 .NET Core 对龙芯的支持情况和对 .NET Core 开发嵌入式的思考 一,遗憾的尝试 二,.NET Core在嵌入式下的几点不足 三,.NET Core 龙芯移植的进展和资料 .NET ...

  9. map的线程安全问题

    为什么HashMap是线程不安全的 总说 HashMap 是线程不安全的,不安全的,不安全的,那么到底为什么它是线程不安全的呢?要回答这个问题就要先来简单了解一下 HashMap 源码中的使用的存储结 ...

  10. window中php的交互模式

    1.配置php的环境变量: 测试: cmd >> php --version 2.在cmd下编写测试脚本 1)  php -r  + php测试代码: 2) php -a + Enter  ...