【LeedCode】3Sum

Given an array S of n integers, are there elements a, b, c 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] ]

分析:

暴力法,三层for循环,时间复杂度为O(n^3),,这显然不是一种好的解决方式

我的做法是先对数组进行排序,这样负数在排在前面(负数越小,则它的绝对值越大),0在中间(如果存在0),正数在后面。排序算法的时间复杂度是O(nlgn)

然后从左往右取负数,从右往左取正数。即从两边向中间扫描。两个数求和的情况则刚好可以两边各取一个数进行判断,对于三个数求和的情况,我们可以先确定一个数,然后再去两个数的和为第一个取出的数的相反数即可。

注:当数组长度小于3时,直接返回null

public List<List<Integer>> func(int[] num) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        int len = num.length;
        if(num==null || len<3){
            return null;
        }
        Arrays.sort(num);

        for(int i =0;i<len-2;i++){
            if(i>0 && num[i] == num[i-1]){
                continue;
            }
            int j = i+1;
            int k = len-1;
            int target = -num[i];
            while(j<k){
                if(num[j]+num[k]==target){
                    result.add(Arrays.asList(num[i],num[j],num[k]));
                    j++;
                    k--;
                    while(j<k && num[j]==num[j-1]){
                        j++;
                    }
                    while(j>k && num[k]==num[k+1]){
                        k--;
                    }
                }else if(num[j]+num[k]>target){
                    k--;
                }else{
                    j++;
                }
            }
        }
        return result;

    }
时间: 2024-11-05 14:44:21

【LeedCode】3Sum的相关文章

【LeetCode】3Sum Closest 解题报告

[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {

【LeetCode】3Sum Closest 解题报告 (Java)

[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {

【Leetcode】3Sum

题目链接:https://leetcode.com/problems/3sum/ 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in no

【LeedCode】String to integer(atoi)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

【leedcode】 Longest Palindromic Substring

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. https://leetcode.com/problems/longest-palindromic-substring/ 求最大回文的长度,其实这道题

【leetcode】3Sum Closest(middle)

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2

【leedcode】 Median of Two Sorted Arrays

https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 =

【Leetcode】3Sum Closest

题目链接:https://leetcode.com/problems/3sum-closest/ 题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exac

【leedcode】longest-substring-without-repeating-characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.