15. 3Sum M

原题:

Given an array S of n integers, are there elements abc in S 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.

For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]
复杂度O(n*n*logn) ,代码太丑,觉得方法有问题,不想讲代码思路了,各种检查一定是方法不好。
这题还有复杂度更低的解法
public class Solution {
    
    class Num_compare implements Comparator<Integer>{  
	    @Override
	    public int compare(Integer o1, Integer o2) {
		    // TODO Auto-generated method stub
		    return o1 - o2;
	    }
    }
    
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        int idx1, idx2, left, right, mid;
        int sum,last;
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        for(idx1 = 0; idx1 < nums.length - 2; ++idx1){
        	while(idx1 < nums.length - 3 && nums[idx1] == nums[idx1+1] && nums[idx1] != 0){
        		if(nums[idx1] != nums[idx1+2]){
        			break;
        		}
        		++idx1;
        	}
        	while(idx1 < nums.length - 3 && nums[idx1] == 0 && nums[idx1] == nums[idx1+1] && nums[idx1] == nums[idx1+2] && nums[idx1] == nums[idx1+3]){
        		++idx1;
        	}
        	System.out.print("idx1 ");
        	System.out.println(idx1);
            for(idx2 = idx1 + 1; idx2 < nums.length - 1 && nums[idx1] + nums[idx2] <= 0; ++idx2){
            	if(idx1 < nums.length - 3 && nums[idx1] == nums[idx1+1] && nums[idx1] != nums[idx2]){
            		++idx1;
            	}
            	
            	while(idx2 < nums.length-2 && nums[idx2] == nums[idx2+1]){
            		if(nums[idx2] != nums[idx2+2] && nums[idx1]+nums[idx2]+nums[idx2+1] == 0){
            			break;
            		}
            		++idx2;
            	}
            	
            	left = idx2+1;
                right = nums.length - 1;
                while(left>=0 && right < nums.length && left <= right){
                    mid = (left + right)/2;
                    sum = nums[idx1] + nums[idx2] + nums[mid];
                    if(sum == 0){
                        List<Integer> res = new ArrayList<Integer>();
                        res.add(nums[idx1]);
                        res.add(nums[idx2]);
                        res.add(nums[mid]);
                        last = nums[mid];
                        result.add(res);
                        break;
                    }
                    else if (sum > 0){
                        right = mid - 1;
                    }
                    else if (sum < 0){
                        left = mid + 1;
                    }
                }                
            }
        }
        return result;
    }
}

更简单的方法是先选择一个数,然后将问题转化为two sum那题
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值