C#与列表相交

时间:2019-12-07 04:46:11

标签: c#

解决leetcode.com问题,我尝试了以下方法,其中包括正确的结果和一个重复的结果。该代码应该找到3个等于0且没有重复的数字的所有可能性。

我正在使用以下if语句在列表列表中查找重复项: if(!returnList.Where(x => x.Intersect(fullResult).Count()== 3).Any())

除了一种情况外,此过滤器都会重复。有谁知道为什么或从列表中消除重复的更好方法?

它将始终不过滤-1,-1、2(这是有效的集合,但返回2次)。

粘贴到控制台应用中以重新创建。

class Program
{
    static void Main(string[] args)
    {
        var output = ThreeSum(new int[] { -1, 0, 1, 2, -1, -4 });
        foreach(var outie in output)
        Console.WriteLine(String.Format("{0}, {1}, {2}", outie[0], outie[1], outie[2]));

        Console.Read();

    }
    static public IList<IList<int>> ThreeSum(int[] nums)
    {

        List<int> lookup = new List<int>();
        foreach (int i in nums)
        {
            lookup.Add(i);
        }

        IList<IList<int>> returnList = new List<IList<int>>();
        for (var i = 0; i < nums.Count(); i++)
        {

            var result = TwoSum(i, lookup);
            if (result != null)
            {
                var fullResult = new List<int>() { nums[i], nums[result[0]], nums[result[1]] };

                if(!returnList.Where(x=>x.Intersect(fullResult).Count()==3).Any())
                {
                    returnList.Add(fullResult);
                }
            }
        }
        return returnList;
    }

    static private int[] TwoSum(int thirdnumIndex, List<int> nums)
    {

        var target = nums[thirdnumIndex];

        for (var i = 0; i < nums.Count(); i++)
        {
            var comp = (target + nums[i]) * -1;
            if (nums.Contains(comp))
            {
                var indexOfComp = nums.IndexOf(comp);

                if (indexOfComp == i || indexOfComp == thirdnumIndex)
                {
                    return null;
                }

                return new int[] { i, indexOfComp };
            }

        }
        return null;
    }
}

1 个答案:

答案 0 :(得分:2)

使用SortSequenceEqual应该可以

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main(string[] args)
    {
        var output = ThreeSum(new int[] { -1, 0, 1, 2, -1, -4 });
        foreach (var outie in output)
        {
            Console.WriteLine($"{outie[0]}, {outie[1]}, {outie[2]}");
        }

        Console.Read();

    }
    private static IList<IList<int>> ThreeSum(int[] nums)
    {
        var lookup = new List<int>(nums);
        var returnList = new List<IList<int>>();

        for (var i = 0; i < nums.Length; i++)
        {
            var result = TwoSum(i, lookup);
            if (result != null)
            {
                var fullResult = new List<int> { nums[i], nums[result[0]], nums[result[1]] };
                fullResult.Sort();
                if (!returnList.Any(b => b.SequenceEqual(fullResult)))
                {
                    returnList.Add(fullResult);
                }
            }
        }
        return returnList;
    }

    private static int[] TwoSum(int thirdnumIndex, List<int> nums)
    {
        var target = nums[thirdnumIndex];

        for (var i = 0; i < nums.Count; i++)
        {
            var comp = (target + nums[i]) * -1;
            if (nums.Contains(comp))
            {
                var indexOfComp = nums.IndexOf(comp);
                if (indexOfComp == i || indexOfComp == thirdnumIndex)
                {
                    return null;
                }

                return new[] { i, indexOfComp };
            }

        }
        return null;
    }
}