예제 #1
0
        public void Test_Generic(string listStr, int k, string expectedStr)
        {
            var head     = ListNodeHelper.BuildList(listStr);
            var sol      = new Solution();
            var res      = sol.SplitListToParts(head, k);
            var expected = ListNodeHelper.BuildListArray(expectedStr);

            Assert.IsTrue(ListNodeHelper.AreEqual(res, expected));
        }
예제 #2
0
        public void Test_Generic(string listStr, int expected)
        {
            var head = ListNodeHelper.BuildList(listStr);

            var sol = new Solution();
            var res = sol.PairSum(head);

            Assert.AreEqual(res, expected);
        }
예제 #3
0
        public void Test_Generic(string inputStr, string expectedStr)
        {
            var head     = ListNodeHelper.BuildList(inputStr);
            var expected = ListNodeHelper.BuildList(expectedStr);

            var sol = new Solution();
            var res = sol.DeleteMiddle(head);

            Assert.IsTrue(ListNodeHelper.AreEqual(res, expected));
        }
예제 #4
0
        public void Test_GenericStr(string listStr, string expectedStr)
        {
            var head     = ListNodeHelper.BuildList(listStr);
            var expected = ListNodeHelper.BuildList(expectedStr);

            var sol = new Solution();
            var res = sol.DeleteDuplicates(head);

            Assert.IsTrue(ListNodeHelper.AreEqual(res, expected));
        }
예제 #5
0
        public void Test_Generic(string inputs, int val, string expectedStr)
        {
            var head = ListNodeHelper.BuildList(inputs);
            var sol  = new Solution();
            var res  = sol.RemoveElements(head, val);

            var expected = ListNodeHelper.BuildList(expectedStr);

            Assert.IsTrue(ListNodeHelper.AreEqual(res, expected));
        }
예제 #6
0
        public void Test_Generic_O1(string listStr, int loopIdx, bool expected)
        {
            var head = ListNodeHelper.BuildList(listStr);

            MakeLoop(head, loopIdx);

            var sol = new Solution_O1();
            var res = sol.HasCycle(head);

            Assert.AreEqual(res, expected);
        }
예제 #7
0
        public void Test_Generic(string inputStr, string expectedStr)
        {
            var head     = ListNodeHelper.BuildList(inputStr);
            var expected = ListNodeHelper.BuildList(expectedStr);

            var sol = new Solution();

            sol.ReorderList(head);

            Assert.IsTrue(ListNodeHelper.AreEqual(head, expected));
        }
예제 #8
0
        public void Test_Stack(string listStr, string expectedStr)
        {
            var head = ListNodeHelper.BuildList(listStr);

            var sol = new Solution();
            var res = sol.OddEvenList(head);

            var expected = ListNodeHelper.BuildList(expectedStr);

            Assert.IsTrue(ListNodeHelper.AreEqual(res, expected));
        }
예제 #9
0
        public void Test_Generic(string strList1, int a, int b, string strList2, string expected)
        {
            var list1        = ListNodeHelper.BuildList(strList1);
            var list2        = ListNodeHelper.BuildList(strList2);
            var expectedList = ListNodeHelper.BuildList(expected);

            var sol = new Solution();
            var res = sol.MergeInBetween(list1, a, b, list2);

            Assert.IsTrue(ListNodeHelper.AreEqual(res, expectedList));
        }
예제 #10
0
        public void Test_Generic(string l1Str, string l2Str, string expectedStr)
        {
            var l1       = ListNodeHelper.BuildList(l1Str);
            var l2       = ListNodeHelper.BuildList(l2Str);
            var expected = ListNodeHelper.BuildList(expectedStr);

            var sol = new Solution();
            var res = sol.AddTwoNumbers(l1, l2);

            Assert.IsTrue(ListNodeHelper.AreEqual(res, expected));
        }
예제 #11
0
        public void Test_Generic <T>(Func <ISolution> constructor, int intersectVal, string listAStr, string listBStr, int skipA, int skipB)
            where T : ISolution
        {
            var listA = ListNodeHelper.BuildList(listAStr);
            var listB = ListNodeHelper.BuildList(listBStr);

            if (intersectVal != 0)
            {
                var a   = listA;
                var b   = listB;
                var skA = skipA;
                var skB = skipB;
                while (skA-- > 0)
                {
                    a = a.next;
                }
                while (skB-- > 1)
                {
                    b = b.next;
                }
                b.next = a;
            }

            var sol = constructor();
            var res = sol.GetIntersectionNode(listA, listB);

            if (intersectVal == 0)
            {
                Assert.AreEqual(null, res);
            }
            else
            {
                Assert.AreEqual(intersectVal, res?.val);
                while (skipA-- > 0)
                {
                    listA = listA.next;
                }
                while (skipB-- > 0)
                {
                    listB = listB.next;
                }
                Assert.AreEqual(res, listA);
                Assert.AreEqual(res, listB);
            }
        }
예제 #12
0
        public void Test_Generic(string[] operations, string inputs)
        {
            var head = ListNodeHelper.BuildList(inputs);
            var sol  = new Solution(head);

            Dictionary <int, int> res = new Dictionary <int, int>();
            int randoms = 0;

            foreach (var op in operations)
            {
                switch (op)
                {
                case "Solution":
                    break;

                case "getRandom":
                    int r = sol.GetRandom();
                    if (res.ContainsKey(r))
                    {
                        res[r]++;
                    }
                    else
                    {
                        res.Add(r, 1);
                    }
                    randoms++;
                    break;
                }
            }

            int avg = randoms / res.Count;

            foreach (var val in res.Values)
            {
                Assert.Greater(val, 0.7 * avg);
                Assert.Less(val, 1.3 * avg);
            }
        }
예제 #13
0
        public void Test_Generic(string list, int pos)
        {
            var head     = ListNodeHelper.BuildList(list);
            var expected = head;

            if (pos == -1)
            {
                expected = null;
            }
            else
            {
                for (int i = 0; i < pos; i++)
                {
                    expected = expected.next;
                }
                ListNodeHelper.Tail(head).next = expected;
            }

            var sol = new Solution();
            var res = sol.DetectCycle(head);

            Assert.AreEqual(res, expected);
        }