Esempio n. 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Path 3x4");
            Console.WriteLine(Path(3, 4));
            Console.WriteLine();
            Console.WriteLine();



            Trees.TreeNode a = new Trees.TreeNode('A');
            Trees.TreeNode b = new Trees.TreeNode('B');
            Trees.TreeNode c = new Trees.TreeNode('C');
            Trees.TreeNode d = new Trees.TreeNode('D');
            Trees.TreeNode e = new Trees.TreeNode('E');
            Trees.TreeNode f = new Trees.TreeNode('F');
            a.Left  = b;
            a.Right = e;
            b.Left  = c;
            b.Right = d;
            e.Right = f;
            Console.WriteLine("    A    ");
            Console.WriteLine(" B     E ");
            Console.WriteLine("C D     F");
            Console.WriteLine();
            Console.Write("DFS: ");
            Trees.TreeDFS(a);
            Console.WriteLine();
            Console.Write("BFS: ");
            Trees.TreeBFS(a);
            Console.WriteLine();
            Console.WriteLine();



            Console.WriteLine("PrintBase FF in base 16");
            PrintBase(0xff, 16);
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("PrintBase FF in base 10");
            PrintBase(0xff, 10);
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("PrintBase FF in base 2");
            PrintBase(0xff, 2);
            Console.WriteLine();
            Console.WriteLine();



            //Console.WriteLine("Perm('ABC')");
            //Perm("ABC".ToCharArray());
            //Console.WriteLine();
            //Console.WriteLine();
            //Console.WriteLine("Perm('ABCD')");
            //Perm("ABCD".ToCharArray());
            //Console.WriteLine();
            //Console.WriteLine();



            // you are given an array of the leafs with each leaf being a single linked list to its
            // hierarchy of parents and all eventually point to <html>
            HTMLParser.Node html = new HTMLParser.Node("html", null);
            HTMLParser.Node p1   = new HTMLParser.Node("p", html); // 1st paragraph node p1's parent is html
            HTMLParser.Node u1   = new HTMLParser.Node("u", p1);   // u1's parent is p1
            HTMLParser.Node p2   = new HTMLParser.Node("p", html); // 2nd paragraphs node p2's parent is html
            HTMLParser.Node u2   = new HTMLParser.Node("u", p2);   // u2's parent is p2
            HTMLParser.Leaf foo  = new HTMLParser.Leaf("foo", p1); // 1st content parent is p1
            HTMLParser.Leaf bar  = new HTMLParser.Leaf("bar", u1); // 2nd content parent is u1
            HTMLParser.Leaf baz  = new HTMLParser.Leaf("baz", u2); // 3rd content parent is u2
            // <html><p>foo<u>bar</u></p><p><u>baz</u></p></html>
            HTMLParser.BuildHTML(new HTMLParser.Leaf[] { foo, bar, baz });
            Console.WriteLine();
            Console.WriteLine();



            int[] values = { -2, -5, 5, 2, -5, 10, -15, 5 };
            Console.WriteLine("Max sum - O(N^3)");
            int startN3 = 0;
            int endN3   = 0;
            int sumN3   = MaxSumN3(values, ref startN3, ref endN3);

            Console.WriteLine("Sum N^3 = {0}. Start = {1}, End = {2}", sumN3, startN3, endN3);
            Console.WriteLine();
            Console.WriteLine("Max sum - O(N^2)");
            int startN2 = 0;
            int endN2   = 0;
            int sumN2   = MaxSumN2(values, ref startN2, ref endN2);

            Console.WriteLine("Sum N^2 = {0}. Start = {1}, End = {2}", sumN2, startN2, endN2);
            Console.WriteLine();
            Console.WriteLine("Max sum - O(N)");
            int startN = 0;
            int endN   = 0;
            int sumN   = MaxSumN(values, ref startN, ref endN);

            Console.WriteLine("Sum N = {0}. Start = {1}, End = {2}", sumN, startN, endN);
            Console.WriteLine();
            Console.WriteLine();



            Console.WriteLine("Linked List");
            LinkedList.Node n1 = new LinkedList.Node(1, "1");
            n1.Next      = new LinkedList.Node(2, "2");
            n1.Next.Next = new LinkedList.Node(3, "3");
            LinkedList.Print(n1);
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Reverse Linked List");
            LinkedList.Print(LinkedList.Reverse(n1));
            Console.WriteLine();
            Console.WriteLine();

            //LLNode n2 = n1;
            //LLNode n3 = DeepClone(n1);
            //n1.Next.Int = -99;
            //n1.Next.String = "A";
            //Print(n2);
            //Print(n3);
            //Console.WriteLine();
            //Console.WriteLine();



            Console.WriteLine("Double Linked List");
            DoubleLinkedList.Node dn1 = new DoubleLinkedList.Node(1, "1");
            DoubleLinkedList.Node dn2 = new DoubleLinkedList.Node(2, "2");
            DoubleLinkedList.Node dn3 = new DoubleLinkedList.Node(3, "3");
            dn1.Next = dn2;
            dn2.Prev = dn1;
            dn2.Next = dn3;
            dn3.Prev = dn2;
            DoubleLinkedList.Print(dn1);
            Console.WriteLine();
            Console.WriteLine();

            //Console.WriteLine("DN1's Int = {0}, String = {1}", dn1.Next.Int, dn1.Next.String);
            //DLLNode dn1a = dn1;
            //DLLNode dn1b = DeepClone(dn1);

            //dn2.Int = -99;
            //dn2.String = "AA";
            //Console.WriteLine("After change - DN1a's Int = {0}, String = {1}", dn1a.Next.Int, dn1a.Next.String);
            //Console.WriteLine("After change - DN1b's Int = {0}, String = {1}", dn1b.Next.Int, dn1b.Next.String);
            //Console.WriteLine();

            //Delete(ref dn1.Next);
            Console.WriteLine("Reverse Double Linked List");
            DoubleLinkedList.Print(DoubleLinkedList.Reverse(dn1));
            Console.WriteLine();
            Console.WriteLine();



            //http://thedailywtf.com/Articles/Nerds,-Jocks,-and-Lockers.aspx
            const int doors = 100;
            int       i     = 1;

            while (i * i <= doors)
            {
                Console.Write(i * i + " ");
                i++;
            }
            Console.WriteLine();
            Console.WriteLine();

            for (int j = 1; j <= Math.Sqrt(doors); j++)
            {
                Console.Write(Math.Pow(j, 2) + " ");
            }
            Console.WriteLine();
            Console.WriteLine();



            int[] input  = { 1, 2, 3, 4, 5 };
            int[] index  = { 0, 1, 2, 2, 2 };
            int[] result = ProductOfArraysWithExclusion(input, index);



            Console.ReadKey();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Test1 t1  = new Test1();
            var   rlt = t1.TowSum2(new int[] { 2, 3, 5, 6 }, 11);

            Console.WriteLine(string.Join(",", rlt));

            Console.WriteLine($"无重复最长字符串长度:{t1.LengthOfLongestSubstring2("pwoyt8wkew")}");
            Console.WriteLine($"数组长度过滤之后:{string.Join(",", t1.removeDistance(new int[] { 1, 2, 3, 4, 5, 5, 7 }, 5))}");

            Console.WriteLine($"A组对B组的最优排序:{string.Join(",", t1.advantageCount(new int[] { 12, 24, 8, 32 }, new int[] { 13, 25, 32, 11 }))}");


            MYLinkList <string> list = new MYLinkList <string>();

            list.Append("abc");
            list.Append("123");
            list.Append("链表数据");
            list.Append("老妈肥肠面");
            list.Append(" ");
            list.Insert("插入数据", 4);//这里的i不是索引
            //list.Delete(" ");
            list.Delete(0);
            Console.WriteLine($"链表总长度为:{list.Length}");

            //list.Clear();
            Node <string> current = list.Head;

            while (current != null)
            {
                Console.WriteLine(current.Data);
                current = current.Next;
            }

            DoubleLinkedList <string> dlinked = new DoubleLinkedList <string>();

            dlinked.Append("双向1");
            dlinked.Append("双向2");
            dlinked.Append("双向3");
            dlinked.Append("双向4");
            //dlinked.Insert("插入双向44", 4);
            Console.WriteLine($"双向链表总长度为:{dlinked.Length()}");
            dlinked.Delete("双向4");
            DLinkedNode <string> d = dlinked.First;

            while (d != null)
            {
                Console.WriteLine(d.Data);
                d = d.Tail;
            }

            Console.WriteLine($"查找目标:{dlinked.Select(34).Data}");

            Console.WriteLine($"求16的平方根{t1.Sqrt(16)}");

            int t    = 2;
            int tail = 3;

            Console.WriteLine(t != (t = tail));

            t1.HashM();
            Console.ReadLine();
        }