[LeetCode]15. 3Sum

比特云影
• 阅读 1157
Given an array nums of n integers, are there elements a, b, c in nums
such that a + b + c = 0? Find all unique triplets in the array which
gives the sum of zero.

Note:

The solution set must not contain duplicate triplets.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]
所有情况应该是o(n^3)的情况,可以通过HashMap优化到o(n^2)

public List<List<Integer>> threeSum(int[] nums) {
    HashMap<Integer,Integer> map=new HashMap();
    HashSet<List<Integer>> set=new HashSet();
    for(int i:nums){
        if(map.containsKey(i)) map.put(i,map.get(i)+1);
        else map.put(i+1);
    }
    for(int i=0;i<nums.length-1;i++){
        for(int j=i+1;j<nums.length){
            int need=-nums[i]-nums[j];
            if(need==nums[i] == need==nums[i]){
                if(map.get(need)<3) continue;
            }else if(need==nums[i] || need==nums[i]){
                if(map.get(need)<2) continue;
            }else{
                if(map.get(need)<1) continue;
            }
            List<Integer> list=Arrays.asList(new int[]{nums[i],nums[j],need});
        }
    }
    return new ArrayList(set);
}

上面的情况是有问题的,会超时比如都是0的情况,就很有很多无用的判断,还有就是没有解决重复性的问题,解决重复性之前的思路是通过set解决,但为了结局超时的情况,决定排序,也能解决重复性的问题。

public List<List<Integer>> threeSum(int[] nums) {
    List<List<Integer>> ret=new ArrayList();
    Arrays.sort(nums);
    Map<Integer,Integer> map=new HashMap();
    for(int i=0;i<nums.length;i++) map.put(nums[i],i);
    for(int i=0;i<nums.length-2;i++){
        if(i>0 && nums[i]==nums[i-1]) continue;
        for(int j=i+1;j<nums.length-1;j++){
            if(j>i+1 && nums[j]==nums[j-1]) continue;
            int need=-nums[i]-nums[j];
            if(need>=nums[j] && map.getOrDefault(need,-1)>j) ret.add(Arrays.asList(nums[i],nums[j],need));
        }
    }
    return ret;
}

再确定一个数后,在后面的有序数组中,也可以通过双指针确定是否存在符合条件的值
public List<List<Integer>> threeSum(int[] nums) {

List<List<Integer>> ret=new ArrayList();
Arrays.sort(nums);
for(int i=0;i<nums.length-2;i++){
    if(i>0 && nums[i]==nums[i-1]) continue;
    int left=i+1;
    int right=nums.length-1;
    while(right>left){
        if(left>i+1 && nums[left]==nums[left-1]) {
            left++;
            continue;
        }
        if(right<nums.length-1 && nums[right]==nums[right+1]){
            right--;
            continue;
        }
        if(nums[i]+nums[left]+nums[right]<0) left++;
        else{
            if(nums[i]+nums[left]+nums[right]==0) ret.add(Arrays.asList(nums[i],nums[left],nums[right]));
            right--;
        }
    }
}
return ret;

}

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
美凌格栋栋酱 美凌格栋栋酱
6个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
待兔 待兔
1年前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
4年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
Postman 使用方法详细介绍
1,下载安装:https://www.getpostman.com/apps2,打开Postman,如图所示:!(https://oscimg.oschina.net/oscnet/00f434cd831f2f74fea6f6d7b86bc46a751.png)3,创建一个接口项目!(https://oscimg.oschina.
Stella981 Stella981
3年前
SpringBoot2.x基础篇:带你了解扫描Package自动注册Bean
!(https://oscimg.oschina.net/oscnet/up6d27eb9793200379ece2b49fc26ed46ef74.png)知识改变命运,撸码使我快乐,2020继续游走在开源界<br/点赞再看,养成习惯<br/给我来个Star吧,点击了解下基于SpringBoot的组件化接口服务落地解决方案(http
Stella981 Stella981
3年前
HTML5新增input标签属性
一.inputtype属性1<formaction""2邮箱<inputtype"email"name""id""<inputtype"submit"value"提交"<br/<br/3手机号码<inputtype"tel"name
Stella981 Stella981
3年前
Nginx反向代理upstream模块介绍
!(https://oscimg.oschina.net/oscnet/1e67c46e359a4d6c8f36b590a372961f.gif)!(https://oscimg.oschina.net/oscnet/819eda5e7de54c23b54b04cfc00d3206.jpg)1.Nginx反
Stella981 Stella981
3年前
Hadoop 气数已尽!逃离复杂性,拥抱云计算
!(https://oscimg.oschina.net/oscnet/355facaec00d46ee851ad87cfdfa754a.gif)作者|MattAsay,译者|杨志昂来源:高效开发运维导读:虽然大数据依然如日中天,但该领域曾经的领头羊Cloudera、Hortonworks和MapR三家公司最近步履
Easter79 Easter79
3年前
SpringBoot2.x基础篇:带你了解扫描Package自动注册Bean
!(https://oscimg.oschina.net/oscnet/up6d27eb9793200379ece2b49fc26ed46ef74.png)知识改变命运,撸码使我快乐,2020继续游走在开源界<br/点赞再看,养成习惯<br/给我来个Star吧,点击了解下基于SpringBoot的组件化接口服务落地解决方案(http
Stella981 Stella981
3年前
LeetCode.1029
这是小川的第383次更新,第412篇原创<br/01看题和准备今天介绍的是LeetCode算法题中Easy级别的第245题(顺位题号是1029)。公司计划采访的人数为2N。将第i个人飞往城市A的费用是i0,将第i个人飞到城市B的费用是费用i1。返回将