コード例 #1
0
            public override bool Equals(object obj)
            {
                Quadraplet o   = (Quadraplet)obj;
                bool       ans = o.a == a && o.b == b && o.c == c && o.d == d;

                return(ans);
            }
コード例 #2
0
            public override bool Equals(object obj)
            {
                Quadraplet o   = (Quadraplet)obj;
                bool       ans = this.a == o.a && this.b == o.b && this.c == o.c && this.d == o.d;

                return(ans);
            }
コード例 #3
0
        public static List <IList <int> > FourSum(int[] nums, int target)
        {
            Dictionary <int, List <couple> > hm = new Dictionary <int, List <couple> >();

            for (int i = 0; i < nums.Length - 1; i++)
            {
                for (int j = i + 1; j < nums.Length; j++)
                {
                    int sum = nums[i] + nums[j];


                    if (!hm.ContainsKey(sum))
                    {
                        List <couple> l = new List <couple>();
                        l.Add(new couple(i, j));


                        hm.Add(sum, l);
                    }

                    else
                    {
                        hm[sum].Add(new couple(i, j));
                    }
                }
            }

            HashSet <Quadraplet> qset = new HashSet <Quadraplet>();

            //IEnumerator en_hm = hm.Keys.GetEnumerator();
            //while (en_hm.MoveNext())
            foreach (int key in hm.Keys)
            {
                //  int currentsum = (int)en_hm.Current;
                int currentsum = key;
                int needed     = target - currentsum;
                if (!hm.ContainsKey(needed))
                {
                    continue;
                }


                List <couple> curentsumList = hm[currentsum];
                List <couple> targetsumList = hm[needed];


                foreach (couple currentListCouple in curentsumList)
                {
                    foreach (couple targetListCouple in targetsumList)
                    {
                        if (currentListCouple.Equals(targetListCouple))
                        {
                            continue;
                        }
                        Quadraplet q = new Quadraplet(nums[currentListCouple.i], nums[currentListCouple.j], nums[targetListCouple.i], nums[targetListCouple.j]);
                        if (qset.Contains(q))
                        {
                            continue;
                        }
                        qset.Add(q);;
                    }
                }
            }

            var ans = qset.Select(x => new List <int>
            {
                x.a,
                x.b,
                x.c,
                x.d
            }).ToList();


            List <IList <int> > ans2 = new List <IList <int> >();

            foreach (Quadraplet eachQuadraplet in qset)
            {
                List <int> singlelist = new List <int>();

                singlelist.Add(eachQuadraplet.a);
                singlelist.Add(eachQuadraplet.b);
                singlelist.Add(eachQuadraplet.c);
                singlelist.Add(eachQuadraplet.d);
                ans2.Add(singlelist);
            }

            return(ans2);
        }
コード例 #4
0
        public static List <List <int> > FourSum(int[] nums, int target)
        {
            HashSet <Quadraplet> allQuadraplets = new HashSet <Quadraplet>();


            for (int i = 0; i < nums.Length - 3; i++)
            {
                for (int j = i + 1; j < nums.Length - 2; j++)
                {
                    for (int k = j + 1; k < nums.Length - 1; k++)
                    {
                        for (int l = k + 1; l < nums.Length; l++)
                        {
                            if (nums[i] + nums[j] + nums[k] + nums[l] == target)
                            {
                                Quadraplet obj = new Quadraplet(nums[i], nums[j], nums[k], nums[l]);
                                if (!allQuadraplets.Contains(obj))
                                {
                                    allQuadraplets.Add(obj);
                                }
                            }
                        }
                    }
                }
            }


            List <IList <int> > ans = new List <IList <int> >();

            foreach (Quadraplet eachQuadraplet in allQuadraplets)
            {
                List <int> singlelist = new List <int>();

                singlelist.Add(eachQuadraplet.a);
                singlelist.Add(eachQuadraplet.b);
                singlelist.Add(eachQuadraplet.c);
                singlelist.Add(eachQuadraplet.d);
                ans.Add(singlelist);
            }
            var ss =
                allQuadraplets.Select(x => new List <int>
            {
                x.a,
                x.b,
                x.c,
                x.d
            }).ToList();



            //foreach (IList<int> i in ans)
            //{
            //    foreach (int k in i)
            //    {
            //        Console.WriteLine(k);
            //    }
            //}


            return(ss);
        }