LeetCode: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 non-descending order. (ie, abc)
  • 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)
 
首先,先把数组排序这样能提高查找的效率。在这里,使用快速排序。其实有3个指针,第一个指针i固定往右移动。另外两个当第一个指针固定时,start指向第一个指针右边一个,end指向结尾,开始查找另外两个值。当*start+*end>-*i,end向左移动一位,小于的时候,start向右移动一位,等于的时候添加到结果中。直到start=end停止。
在其实忽略了一个问题,就是有相同的答案我们还是会添加进去。这里使用一个String content,把添加到结果的排列以字符方式添加到content中,添加前要判断content是否包含,如果包含了就跳过。
还有一个就是可以节约时间的是当i指针值大于0了说明所有值都大于0,则停止返回。还有个是,当下一个i与前一个指针值相同时,就跳到下一个。不要做重复的动作。

public class Solution {
         public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
       
        ArrayList<ArrayList<Integer>> sum = new ArrayList<ArrayList<Integer>>();
        int length = num.length;
        if(length < 3)return sum;
        sort(num,0,length-1);
        String content = "";
        for(int i=0;i<length-2;i++)
        {
            int value = num[i];
            if(value>0)
            {
                break;
            }
            else if(i>0&&num[i] == num[i-1])
            {}
            else
            {
                value = -value;
                int start = i+1;
                int end = length -1;
                while(start<end)
                {
                    if(num[start]+num[end]==value)
                    {
                        if(i>0&&num[i] == num[i-1])
                        {
                            start++;
                        }
                        else
                        {
                            String temp = "" + num[i] +"," + num[start] + "," + num[end] + ",";
                            if(!content.contains(temp))
                            {
                                ArrayList<Integer> zero = new ArrayList<Integer>();
                                zero.add(num[i]);
                                zero.add(num[start]);
                                zero.add(num[end]);
                                sum.add(zero);
                                content = content + temp;
                            }
                            start++;
                        }
                       
                    }
                    else if(num[start]+num[end]>value)
                    {
                        end--;
                    }
                    else if(num[start]+num[end]<value)
                    {
                        start++;
                    }
                }
            }
        }
        return sum;
       
    }
    public void sort(int[] num,int start,int end)
    {
        if(start<end)
        {
            int p = partition(num,start,end);
            sort(num,start,p-1);
            sort(num,p+1,end);
        }
       
    }
    public int partition(int[] num,int start,int end)
    {
        if(end - start==0)return start;
        int key = num[start];
        while(start<end)
        {
            while(end>start&&num[end]>key)
            {
                end -- ;
            }
            num[start] = num[end];
            while(start<end&&num[start]<=key)
            {
                start ++;
            }
            num[end] = num[start];
        }
        num[start]=key;
        return start;
    }
}

 
 
 
 
 
 
 
posted on 2014-05-22 21:18  JessiaDing  阅读(134)  评论(0编辑  收藏  举报