码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode:3Sum

时间:2014-05-26 14:02:27      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:des   c   class   code   tar   a   

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;
    }
}

 
 
 
 
 
 
 

LeetCode:3Sum,布布扣,bubuko.com

LeetCode:3Sum

标签:des   c   class   code   tar   a   

原文地址:http://www.cnblogs.com/jessiading/p/3746480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!