leetcode 15:3 Sum (C#语言版)

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:

  • Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
  • 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)
刚看到这题时,准备采用list的reverse功能(我发现我用上一个方法后就一定会把这个方法用到死啊。。。),然后再去分三层循环遍历求出结果,很白痴的暴力求解方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _3Sum
{
    class ThreeSum
    {
        public IList<int>[] Three(int[] nums)
        {

            IList<int>[] myLL = new List<int>[100];
            int m = 0;
            for(int i=0;i<nums.Length-2;i++)
            {
                myLL[m] = new List<int>();
                myLL[m].Add(nums[i]);
                int num1 = 0 - nums[i];
                for(int j=i+1;j<nums.Length-1;j++)
                {
                    myLL[m].Add(nums[j]);
                    int num2 = num1 - nums[j];
                    for(int k=j+1;k<nums.Length;k++)
                    {
                        if(nums[k]==num2)
                        {
                            myLL[m].Add(nums[k]);
                            break;
                        }
                    }
                    if (myLL[m].Count == 2)
                    {
                        myLL[m].Clear();
                        myLL[m].Add(nums[i]);
                        if(i==4)
                        {
                            myLL[m].Clear();
                        }
                    }
                    else
                    {
                        m += 1;
                        myLL[m] = new List<int>();
                        myLL[m].Add(nums[i]);
                        if (i == nums.Length - 3)
                            myLL[m].Clear();

                    }
                }
                

            }
            return myLL;
        }
    }
}
时间负责度0(n^3),提交结果后不出所料的运行超时,心累啊,好比当年算数学题不管三七二十一先算出结果结果老师欻欻几道给我判错的感觉。无奈又去把别人的结果进行了改编。。。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _3Sum
{
    class AnotherThreeSum
    {
        public IList<IList<int>> threeSum(int[] nums) {

        IList<IList<int>> result = new List<IList<int>>();
        List<int> myL = nums.ToList();
        myL.Sort();
        int[] num = myL.ToArray();

        int start,end,temp;

        for(int i=0;i<num.Length;i++){

            if( i!=0 && num[i]==num[i-1] )continue;     //num 1:only reserve first of all same values 

                int current=num[i];

                start=i+1;

                end=num.Length-1;

 

            while(start<end){

                if(start!=i+1 && num[start]==num[start-1] ){        //num 2:only reserve first of all same values

                    start++;

                    continue;

                }

                temp=num[start]+num[end];

 

                if(temp==-current){                 //find

                    List<int> list = new List<int>(3);

                    list.Add(current);

                    list.Add(num[start]);

                    list.Add(num[end]);

                    result.Add(list);

                    start++;end--;

                }else if(temp>-current)end--;     

                else start++;                     

            }

        }

        return result;

    }

    }
}

方法很巧妙,用的是夹逼方法,然而对于我这个菜鸟来说是万万也想不到这样的方法的。思路源于Concise O(N^2) Java solution - Leetcode Discuss https://leetcode.com/discuss/23638/concise-o-n-2-java-solution


你可能感兴趣的:(LeetCode,.net,算法,C#)