[LeetCode]15. 3Sum

栏目: 编程工具 · 发布时间: 6年前

内容简介:上面的情况是有问题的,会超时比如都是0的情况,就很有很多无用的判断,还有就是没有解决重复性的问题,解决重复性之前的思路是通过set解决,但为了结局超时的情况,决定排序,也能解决重复性的问题。再确定一个数后,在后面的有序数组中,也可以通过双指针确定是否存在符合条件的值public List<List<Integer>> threeSum(int[] nums) {

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;

}


以上所述就是小编给大家介绍的《[LeetCode]15. 3Sum》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

产品故事地图

产品故事地图

唐娜·理查(Donna Lichaw) / 向振东 / 机械工业出版社 / 2017-6 / 49.9元

本书一共8章,分为三个部分:第1-2章讲述故事的作用、你该如何运用产品故事来吸引顾客,不是通过讲故事,而是创造故事。第3-5章阐述了不同情境和客户生命周期中的产品故事类型。第6-8章进一步研究如何在战略和策略层面发现、提升、用好你的产品故事。 《产品故事地图》写给那些想要通过创造出顾客喜欢用、经常用而且会推荐给别人用的产品来吸引客户的人。这里的“产品”包括网页、软件、APP、数字化或非数字化......一起来看看 《产品故事地图》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

SHA 加密
SHA 加密

SHA 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具