コード例 #1
0
    public static int getLoopSize(LoopDetector.Node startNode)
    {
        var list = new HashSet <LoopDetector.Node>();

        LoopDetector.Node node = startNode;
        LoopDetector.Node loopNode;

        while (true)
        {
            node = node.next;

            if (list.Contains(node))
            {
                loopNode = node;
                break;
            }

            list.Add(node);
        }

        int count = 0;

        while ((node = node.next) != loopNode)
        {
            count++;
        }

        return(count + 1);
    }
コード例 #2
0
        public void FourNodesWithLoopSize3()
        {
            var n1 = new LoopDetector.Node();
            var n2 = new LoopDetector.Node();
            var n3 = new LoopDetector.Node();
            var n4 = new LoopDetector.Node();

            n1.next = n2;
            n2.next = n3;
            n3.next = n4;
            n4.next = n2;
            Assert.AreEqual(3, LoopDetector.getLoopSize(n1));
        }
コード例 #3
0
        static void Main(string[] args)
        {
            var n1 = new LoopDetector.Node();
            var n2 = new LoopDetector.Node();
            var n3 = new LoopDetector.Node();
            var n4 = new LoopDetector.Node();

            n1.next = n2;
            n2.next = n3;
            n3.next = n4;
            n4.next = n2;

            Console.WriteLine(Kata.getLoopSize(n1));
        }
コード例 #4
0
        public static int getLoopSize(LoopDetector.Node startNode)
        {
            var nodeDict = new Dictionary <LoopDetector.Node, int>();

            LoopDetector.Node currentNode = startNode;
            int stepCounter = -1;

            while (!nodeDict.ContainsKey(currentNode))
            {
                nodeDict.Add(currentNode, ++stepCounter);
                currentNode = currentNode.next;
            }

            return(stepCounter - nodeDict[currentNode] + 1);
        }
コード例 #5
0
        public void More_nodes()
        {
            var n1 = new LoopDetector.Node();
            var n2 = new LoopDetector.Node();
            var n3 = new LoopDetector.Node();
            var n4 = new LoopDetector.Node();
            var n5 = new LoopDetector.Node();

            n1.next = n2;
            n2.next = n3;
            n3.next = n4;
            n4.next = n5;
            n5.next = n3;

            Assert.AreEqual(3, Kata.getLoopSize(n1));
        }
コード例 #6
0
    public static int getLoopSize(LoopDetector.Node startNode)
    {
        Dictionary <LoopDetector.Node, int> dict = new Dictionary <LoopDetector.Node, int>();
        int len = 0, i = 0;

        LoopDetector.Node nextNode = startNode;
        while (nextNode != null)
        {
            dict.Add(nextNode, i++);
            nextNode = nextNode.next;
            if (dict.ContainsKey(nextNode))
            {
                len = i - dict[nextNode];
                break;
            }
        }
        return(len);
    }
コード例 #7
0
    public static int getLoopSize_dictionary(LoopDetector.Node startNode)
    {
        var dictionary   = new Dictionary <LoopDetector.Node, int>();
        var currentNode  = startNode.next;
        var currentIndex = 0;

        do
        {
            if (dictionary.TryGetValue(currentNode, out int index))
            {
                return(currentIndex - index);
            }
            else
            {
                dictionary.Add(currentNode, currentIndex++);
            }
            currentNode = currentNode.next;
        } while (true);
    }
コード例 #8
0
    //public static int getLoopSize(LoopDetector.Node startNode)
    //{
    //    var hash = new HashSet<LoopDetector.Node> { startNode };

    //    var currentNode = startNode.next;
    //    var duplicateFound = false;
    //    do
    //    {
    //        if (hash.Contains(currentNode))
    //            duplicateFound = true;
    //        else
    //        {
    //            hash.Add(currentNode);
    //            currentNode = currentNode.next;
    //        }
    //    } while (!duplicateFound);


    //    var found = false;
    //    var startOfLoopNode = currentNode;
    //    var loopSize = hash.Count;
    //    currentNode = startNode;
    //    do
    //    {
    //        if (currentNode == startOfLoopNode)
    //            found = true;
    //        else
    //        {
    //            loopSize--;
    //            currentNode = currentNode.next;
    //        }

    //    } while (!found);

    //    return loopSize;
    //}

    public static int getLoopSize22(LoopDetector.Node startNode)
    {
        var hash = new HashSet <LoopDetector.Node> {
            startNode
        };
        var currentNode    = startNode.next;
        var duplicateFound = false;

        do
        {
            if (hash.Contains(currentNode))
            {
                duplicateFound = true;
            }
            else
            {
                hash.Add(currentNode);
                currentNode = currentNode.next;
            }
        } while (!duplicateFound);


        var found           = false;
        var startOfLoopNode = currentNode;
        var loopSize        = hash.Count;

        currentNode = startNode;
        do
        {
            if (currentNode == startOfLoopNode)
            {
                found = true;
            }
            else
            {
                loopSize--;
                currentNode = currentNode.next;
            }
        } while (!found);

        return(loopSize);
    }
コード例 #9
0
        public static int getLoopSize(LoopDetector.Node startNode)
        {
            System.Collections.Generic.Dictionary <LoopDetector.Node, int> previousNodes = new System.Collections.Generic.Dictionary <LoopDetector.Node, int>();

            var currentNode  = startNode;
            var currentIndex = 0;

            while (true)
            {
                int loopStartIndex;
                currentIndex++;
                if (previousNodes.TryGetValue(currentNode, out loopStartIndex))
                {
                    return(currentIndex - loopStartIndex);
                }

                previousNodes[currentNode] = currentIndex;
                currentNode = currentNode.next;
            }
        }
コード例 #10
0
    public static int getLoopSize(LoopDetector.Node startNode)
    {
        HashSet <LoopDetector.Node> previousNodes = new HashSet <LoopDetector.Node>();

        while (previousNodes.Add(startNode))
        {
            startNode = startNode.next;
        }

        int size = 1;

        LoopDetector.Node currentNode = startNode;

        while (currentNode.next != startNode)
        {
            size++;
            currentNode = currentNode.next;
        }

        return(size);
    }
コード例 #11
0
    public static int getLoopSize(LoopDetector.Node startNode)
    {
        var conditionalWeakTable = new ConditionalWeakTable <LoopDetector.Node, IndexedNode>();
        var currentNode          = startNode.next;
        var currentKey           = 0;

        do
        {
            if (conditionalWeakTable.TryGetValue(currentNode, out IndexedNode b))
            {
                return(currentKey - b.Index + 1);
            }
            else
            {
                currentKey++;
                conditionalWeakTable.Add(currentNode, new IndexedNode()
                {
                    Index = currentKey, Node = currentNode
                });
                currentNode = currentNode.next;
            }
        } while (true);
    }
コード例 #12
0
        public static int GetLoopSize(LoopDetector.Node startNode)
        {
            var turtle = startNode;
            var rabbit = startNode;

            int colisionCounter = 0;
            int loopLength      = 0;

            while (rabbit.Next != null)
            {
                turtle = turtle.Next;
                rabbit = rabbit.Next.Next;

                if (turtle == rabbit)
                {
                    colisionCounter++;
                }

                if (colisionCounter == 1)
                {
                    loopLength++;
                }

                if (turtle != rabbit)
                {
                    continue;
                }

                if (colisionCounter == 2)
                {
                    break;
                }
            }

            return(loopLength);
        }