昨晚,在做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. JavaScript BOM学习

    Mirror王宇阳 2019年11月13日 [首发] 数日没有更新博文了,觉得不好意思了!这不是,整理了一下JavaScript的一下BOM笔记资料,今天贡献出来!(HTML DOM也会随后整理发表) ...

  2. ValueError: zero-size array to reduction operation maximum which has no identity

    数据打印到第530行之后出现以下异常,求解!

  3. Redis实战--Jedis实现分布式锁

    echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!!! 分布式 ...

  4. Xshell选中的同时把内容复制到剪贴板

    1.设置对话框 工具 -> 选项 -> 键盘和鼠标 -> 将选定的文本自动复制到剪贴板 2.贴图如下 2.1.打开设置对话框 2.2.设置键盘鼠标,左键复制

  5. 用PHP+Redis实现延迟任务,实现自动取消订单

    简单定时任务解决方案:使用redis的keyspace notifications(键失效后通知事件) 需要注意此功能是在redis 2.8版本以后推出的,因此你服务器上的reids最少要是2.8版本 ...

  6. VMware虚拟机Linux中增加磁盘空间的扩容操作

    VMwareware虚拟机安装的Red Hat Enterprise Linux系统剩余空间不足,造成软件无法正常安装.如果重新装一遍系统就需要重新配置好开发环境和软件的安装配置.结合自己的实践,总结 ...

  7. 100天搞定机器学习|Day56 随机森林工作原理及调参实战(信用卡欺诈预测)

    本文是对100天搞定机器学习|Day33-34 随机森林的补充 前文对随机森林的概念.工作原理.使用方法做了简单介绍,并提供了分类和回归的实例. 本期我们重点讲一下: 1.集成学习.Bagging和随 ...

  8. php使用cUrl方法 get、post请求

    php使用curl方法,请确保已经开启curl扩展.传送门:http://www.cnblogs.com/wgq123/p/7450667.html /**Curl请求get方法 *@$url Str ...

  9. mysql的属性zerofill

    一.字段中zerofill属性的类似定义方式 SQL语句:字段名 int(M) zerofill 二.zerofill属性的作用 1.插入数据时,当该字段的值的长度小于定义的长度时,会在该值的前面补上 ...

  10. 20191031-9 beta week 1/2 Scrum立会报告+燃尽图 07

    此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/9917 一.小组情况 队名:扛把子 组长:孙晓宇 组员:宋晓丽 梁梦瑶 韩昊 ...