示例#1
0
        static void Main(string[] args)
        {
            //string input = "[[()]{}]";
            //StackProblems sp = new StackProblems();
            //int count = sp.CheckBracket(input);
            //if (count > 0)
            //{
            //    Console.WriteLine("False");
            //}
            //else
            //{
            //    Console.WriteLine("True");
            //}
            //string check = "e";
            //while (check != "q")
            //{
            //    StringHelper shelp = new StringHelper();
            //    Console.WriteLine("Enter Input to test");
            //    string input5 = Console.ReadLine();

            //    Console.WriteLine("Is the input can be palindrom ? {0}", shelp.CanBePalindrom(input5));
            //    Console.WriteLine("Enter q to exit anyother character to continue for other input");
            //    check = Console.ReadLine();
            //}
            ArrayProblem arr = new ArrayProblem();


            //int i = arr.FindMajority(new int[] { 2, 6, 1, 4, 2, 2, 2 });
            //int i = arr.NumberOddOccurrence(new int[] { 2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2} );
            int[] input = new int[] { 1, 2, 3, 4, 5, 6 };
            arr.ReverseArray(input);
            for (int i = 0; i < input.Length; i++)
            {
                Console.WriteLine(input[i]);
            }
            //Console.WriteLine(input);


            Console.ReadLine();
        }
示例#2
0
 public void ToLowerCase()
 {
     var result = ArrayProblem.ToLowerCase("gaurava");
 }
示例#3
0
 public void SumaAndProblem()
 {
     ArrayProblem.ProductAndSum(234);
 }
示例#4
0
 public void TransposeMatrix()
 {
     ArrayProblem.Transpose(new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 } });
 }
示例#5
0
        public void SmallerNumbersThanCurrent()
        {
            var nums = new int[] { 8, 1, 2, 2, 3 };

            nums = ArrayProblem.SmallerNumbersThanCurrent(nums);
        }
示例#6
0
 public void MinSetSize()
 {
     int[] arr    = { 7, 7, 7, 7, 7, 7 };
     var   result = ArrayProblem.MinSetSize(arr);
 }
示例#7
0
        public void BubbleSorting()
        {
            var nums = new int[] { 100, 200, 29, 8, 20, 1, 3 };

            nums = ArrayProblem.BubbleSort(nums);
        }
示例#8
0
 public void DuplicateZeros()
 {
     int[] arr = new int[] { 0, 0, 0 };
     ArrayProblem.DuplicateZeros(arr);
 }
示例#9
0
 public void MoveZeros()
 {
     int[] arr = new int[] { 1, 0, 2, 3, 0, 4 };
     ArrayProblem.MovingZeroEnd(arr);
 }
示例#10
0
 public void Rotate()
 {
     int[] arr = new int[] { 1, 2, 3 };
     ArrayProblem.Rotate(arr, 4);
 }
示例#11
0
 public void Reverse()
 {
     int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7 };
     ArrayProblem.Reverse(arr, 0, arr.Length - 1);
 }
示例#12
0
 public void RemoveDuplicates()
 {
     int[] arr   = new int[] { 1, 1, 2, 3, 4, 4, 5 };
     int   count = ArrayProblem.RemoveDuplicates(arr);
 }
示例#13
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            int[] arr = { 1, 2, 3, 3, 4, 5, 5, 8, 10, 10 };
            //var a = BubbleSort.GetSorted(arr);
            // QuickSort.GetSorted(arr);
            MyHeap.BuildHead();
            //foreach (var x in a)
            //{
            //    Console.WriteLine(x);
            //}

            var a = removeDuplicates(arr, arr.Length);

            // Print updated array
            for (int i = 0; i < a; i++)
            {
                Console.Write(arr[i] + " ");
            }

            var b = BianrySearch(arr, 225, 0, arr.Length - 1);

            Console.WriteLine(b);

            BinaryTree bt    = new BinaryTree();
            BinaryTree bt1   = new BinaryTree();
            var        root  = bt.Insert(null, 10);
            var        root1 = bt1.Insert(null, 10);

            /*root = bt.Insert(root, 20);
             * root = bt.Insert(root, 8);
             * root = bt.Insert(root, 9);
             * root = bt.Insert(root, 15);
             * root = bt.Insert(root, 25);
             */
            bt.Insert(root, 20);
            bt.Insert(root, 8);
            bt.Insert(root, 9);
            bt.Insert(root, 15);
            bt.Insert(root, 25);
            bt.Insert(root, 12);
            bt.Insert(root, 5);

            bt1.Insert(root1, 20);
            bt1.Insert(root1, 8);
            bt1.Insert(root1, 9);
            bt1.Insert(root1, 15);
            bt1.Insert(root1, 25);
            //bt1.Insert(root1, 12);
            bt1.Insert(root1, 7);

            TwoNodes tn = new TwoNodes {
                First = bt.RootNode, Second = bt1.RootNode
            };

            Console.WriteLine("Is_BST_Identical_Iterative  ::{0} ", bt.Is_BST_Identical_Iterative(tn));
            Console.WriteLine("Is_BST_Identical  ::{0} ", bt.isIdentical(bt.RootNode, bt1.RootNode));
            bt1.Height_Tree_Iterative(bt1.RootNode);

            Console.WriteLine("PreOrder");
            bt.PreOrder(bt.RootNode);
            Console.WriteLine("");
            Console.WriteLine("InOrder");
            bt.InOrder(bt.RootNode);
            Console.WriteLine("");
            Console.WriteLine("PostOrder");
            bt.PostOrder(bt.RootNode);

            bt.CreateBinaryTreeFromInAndPreOrder();

            Console.WriteLine("");
            Console.WriteLine("Find");
            var f = bt.Find(115, bt.RootNode);

            Console.WriteLine(f != null ? f.data : 0);

            Console.WriteLine("");
            Console.WriteLine("MinValue");

            var m = bt.GetMinValue(bt.RootNode);

            Console.WriteLine(m);
            int pos = 6;

            bt.GetKthMinValue(bt.RootNode, ref pos);
            //bt.LevelOrder(bt.RootNode);
            //bt.Inorder_Iterative(bt.RootNode);
            //bt.Preorder_Iterative(bt.RootNode);
            //bt.Postorder_Iterative(bt.RootNode);
            //bt.Get_Inorder_Predecessor(bt.RootNode);
            //bt.Get_Inorder_Successor(bt.RootNode);

            Console.WriteLine("\nInvert binary");
            bt.InvertBinary(bt.RootNode);
            bt.LevelOrder(bt.RootNode);

            Node ln = new Node {
                data = 20
            };
            Node rn = new Node {
                data = 30
            };
            Node no = new Node {
                data = 10, Left = ln, Right = rn
            };

            // Console.WriteLine("isBST :: {0}", bt.isBST(no, int.MinValue) );
            Console.WriteLine("isBST :: {0}", bt.isBinarySearch(no));

            AVLTree at = new AVLTree();

            at.Avl_execute();

            int[] arr1 = { 5, 6, 8, 9, 10, 12, 15, 20 };

            GetTriplets(arr1, 30);
            GetSumDobles(arr1, 15);
            GetSumDobles_Linear(arr1, 15);

            Console.WriteLine("KnapSack");
            int[] val = new int[] { 60, 120, 100 };
            int[] wt  = new int[] { 10, 30, 20 };
            int   Cap = 50;
            int   n   = val.Length;

            int[,] dp = new int[n + 1, Cap + 1];
            KnapSack k = new KnapSack();

            Console.WriteLine(k.KnapSack_Binary(Cap, wt, val, n, dp));
            k.KnapSack_Fraction(Cap, wt, val, n);

            Console.WriteLine("Pattern");
            Pattern.SearchPattern("AABAACAADAABAAABAACAAD", "CAAD");
            Pattern.Binary_MultipleOf3("110100000101");

            Console.WriteLine("Largest Sum Contiguous Subarray");
            int[] aa = { 1, 2, 3, -2, 5 };
            Pattern.maxSubArraySum(aa);
            Console.WriteLine("Missing elements");

            int[] aa1 = { 1, 2, 3, 5, 7, 8, 10 };
            Pattern.FindMissingElements(aa1);

            int[] aa2 = { 1, 2, 3, 1, 1 };
            Pattern.FindMajorityElement(aa2);

            //Search in a Rotated Array
            int[] aa3 = { 5, 6, 7, 8, 9, 10, 1, 2, 3, 4 };
            Pattern.SearchRotatedArray(aa3, 10);

            //Linked List
            LinkedList lst = new LinkedList();

            lst.InsertFront(10);
            lst.InsertLast(20);
            lst.InsertFront(5);
            lst.InsertLast(25);
            lst.InsertLast(35);
            lst.PrintList(lst);

            lst.FindMiddleElement(lst);

            Problem_30 p30 = new Problem_30();

            p30.FindSingeNumber();
            p30.HappyNumber();
            p30.Longest_Sum_Subarray();
            p30.FindSubarraySum();
            p30.MoveZeroRight();
            p30.MaxProfit();
            p30.IsAnagram();
            p30.StringSorting("kgadeA");
            p30.GroupingAnagrams();
            p30.CountingElement();
            p30.BackspaceStingCompare();
            //p30.LastStoneWeight();
            p30.SubArrayWithEqualZeroAndOne();
            p30.StringShif();
            p30.ProductOfArrayExceptSlef();
            Console.WriteLine("\nValid parenthesis:: {0}", p30.ValidParenthesis());
            p30.NumberOfIsland();
            p30.LeftmostColumnWithAtleastOne();
            p30.BitwiseAnd();
            p30.JumpingProblem();
            p30.LongestcommonSubsequence();
            p30.CountPairsGivenSum();
            p30.MaximumPathSum(bt.RootNode);
            p30.ValidSequence(bt.RootNode);
            p30.MinFromSatck();
            p30.MinimumPathSum();
            p30.HistogramMaxRectangle();
            //p30.Print_Combination_R_Elements_Array();

            ///All Array Problem
            ///
            ArrayProblem ar = new ArrayProblem();

            ar.Find_And_Print_SubArraySum();
            ar.SortBinaryArray();
            ar.FindDuplicate();
            ar.LongestConsecutiveSubArray();
            ar.MaximumSumSubArray();
            ar.MinimuSumSubArray();
            ar.SubArrayWithEqualZeroAndOne();
            ar.SortArrayOnlyZeroOneTwos();
            ar.Merge2SortedArray();
            ar.MergeArray_ReplaceZeroValue();
            ar.Find_Index_MaxLength_ContinuousOnes();
            ar.ProductOfTwoIntArray();
            ar.Suffle();
            ar.RearrangeArrayHighAndLow();
            ar.EquilibriumIndex();
            ar.FindMajorityElement();
            ar.MoveAllZeroToEnd();
            ar.ProductOfArrayWithotSlef();
            ar.BitonicSubArray();
            ar.FindMaximumDifference();
            ar.MaximumSumCircularSubArray();
            ar.FindSumMimumSubArray_Given_Size();
            ar.FindSumSubArray_Given_Sum();
            ar.LengthOfSmallestSubarray_SumOfElements_GreaterThanNumber();
            ar.Find_Smallest_Window_Will_Make_Entire_Array_Sorted();
            ar.TrappingRainWaterwithin_given_set_bars();
            ar.Find_Maximum_Sum_Subsequence_With_NO_Adjacent();
            ar.MinimumNumber_Platform();
            ar.LengthOf_Continuous_Same_Sum_Two_Binary_Array();
            ar.Find_Number_Rotations();
            ar.RodCutting();
            ar.Find_kth_Smallest_Element();
            ar.FindAllCombinations();

            //Trie data structure
            Trie tr = new Trie();

            string[] words = { "abc", "abgl", "cdf", "abcd", "lmn" };
            //string[] words = { "abc"};
            foreach (string st in words)
            {
                tr.Insert(st);
            }

            Console.WriteLine("\nIs string present {0} ", tr.Search("cdf"));
            tr.AutoComplete("ab");

            // List of graph edges as per above diagram
            List <Edge> edges = new List <Edge> {
                // Notice that node 0 is unconnected node
                new Edge(1, 2), new Edge(1, 7), new Edge(1, 8),
                new Edge(2, 3), new Edge(2, 6), new Edge(3, 4),
                new Edge(3, 5), new Edge(8, 9), new Edge(8, 12),
                new Edge(9, 10), new Edge(9, 11)
            };

            // Set number of vertices in the graph (0-12)
            int N = 13;

            // create a graph from edges
            Graph graph = new Graph(edges, N);

            // stores vertex is discovered or not
            bool[] discovered = new bool[N];

            // Do DFS traversal from all undiscovered nodes to
            // cover all unconnected components of graph
            for (int i = 0; i < N; i++)
            {
                if (!discovered[i])
                {
                    graph.DFS(graph, i, discovered);
                }
            }

            //Matrix
            Matrix mt = new Matrix();

            mt.Print_SpiralOrder();
            mt.Rotation_90();
            mt.FloodFill();
        }
示例#14
0
        static void Main(string[] args)
        {
            int[] arr = new int[] { 1, 1, 2 };
            int   res = ArrayProblem.Duplicate1(ref arr);

            MyWrite(arr);

            string s = "loveleetcode";

            Console.WriteLine(StringProblem.FirstUniqChar(s));

            Console.ReadKey();
            int oriInt = 123;

            Console.WriteLine(StringProblem.RevertInt(oriInt));
            Console.ReadKey();

            var revert = StringProblem.RevertString("hi, i am lucy.");

            Console.WriteLine(revert);
            Console.Read();

            var resArr = ArrayProblem.TwoSum(new int[] { 2, 7, 11, 15 }, 18);

            MyWrite(resArr);

            //int[] arr = new int[] { 1, 1, 2 };
            //int res = ArrayProblem.Duplicate(ref arr);
            //for(int i = 0; i < res; ++i)
            //{
            //    Console.WriteLine(arr[i]);
            //}

            Console.ReadKey();

            Console.WriteLine("斐波拉契数列,第10位");
            Console.WriteLine(Fibonacci.GetSeq(10));
            //Console.ReadKey();

            CStack.Output();
            CArray.Output();

            Console.WriteLine(".....................");
            ScoreArray sa = new ScoreArray();

            sa.scores = new int[] { 99, 89, 88, 78, 55 };
            Console.WriteLine("数组: [99, 89, 88, 78, 55]");
            Console.WriteLine("avg: {0}", sa.GetAvg());
            Console.WriteLine("max: {0}", sa.GetMax());
            Console.WriteLine("min: {0}", sa.GetMin());

            Console.WriteLine(".....................");
            Console.WriteLine("数组:[1, 3, 4, 6, 9]");
            int[] sales = new int[] { 1, 3, 4, 6, 9 };
            Console.WriteLine("和: {0}", Common.GetTotalCase(sales));

            Console.WriteLine(".....................");
            Console.WriteLine("数组:[5, 1, 22, 7,9]");
            double[] a = new double[] { 5, 1, 22, 7, 9 };
            Console.WriteLine("中间数: {0}", Common.Middle(a));

            Console.WriteLine(".....................");
            Console.WriteLine("Fibonacci sequence");
            foreach (long fib in FibonacciGenerator.GetSequence(11))
            {
                Console.WriteLine(fib);
            }
            Console.WriteLine(".....................");
            Console.WriteLine("斐波拉契数列第10位: {0}", Fibonacci.GetSequence(10));

            Console.WriteLine(".....................");
            FactoryDemo();
            Console.ReadKey();
        }
示例#15
0
 public void Fibonacci()
 {
     var result = ArrayProblem.Fib(2);
 }
示例#16
0
 public void MaxConsecutive1()
 {
     int[] arr = new int[] { 1, 0, 1, 1, 0 };
     ArrayProblem.MaxConsecutiveOne(arr);
 }
示例#17
0
 public void DeckRevealedIncreasing()
 {
     int[] arr    = { 17, 13, 11, 2, 3, 5, 7 };
     var   result = ArrayProblem.DeckRevealedIncreasing(arr);
 }
示例#18
0
 public void MissingNumber()
 {
     int[] arr = new int[] { 1, 0 };
     ArrayProblem.MissingNumber(arr);
 }
示例#19
0
        public void SelctionSorting()
        {
            var nums = new int[] { 100, 200, 29, 8, 20, 1, 3 };

            nums = ArrayProblem.SelectionSort(nums);
        }
示例#20
0
 public void Decompress()
 {
     int[] arr = new int[] { 1, 2, 3, 4 };
     ArrayProblem.Decompress(arr);
 }
示例#21
0
        public void BubbleSortingUsingRecursion()
        {
            var nums = new int[] { 100, 200, 29, 8, 20, 1, 3 };

            nums = ArrayProblem.BubbleSortUsingRecursion(nums, nums.Length);
        }
示例#22
0
 public void SumZero()
 {
     ArrayProblem.SumZero(5);
 }
示例#23
0
 public void CreateTargetArray()
 {
     var nums   = new int[] { 0, 1, 2, 3, 4 };
     var index  = new int[] { 0, 1, 2, 2, 1 };
     var Target = ArrayProblem.CreateTargetArray(nums, index);
 }
示例#24
0
        static void Main(string[] args)
        {
            //LCM lcm = new LCM();
            //var result = lcm.BeetweenTwoNumber(8, 5);

            int[] arrs = { 10, 1, 31, 52, 2, 40, 4, 5 };
            //LeanerSearch l = new LeanerSearch();
            //var result = l.Search(arrs, 5);

            //int[] arrs = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            //BinarySearch bs = new BinarySearch();
            //var result = bs.Search(arrs, 2);


            //SelectionSort.Sort(arrs);
            //BubbleSort.Sort(arrs);
            //InsertionSort.Sort(arrs);
            //MergeSort.Sort(arrs);



            //Palindrome palindrome = new Palindrome();
            //Console.WriteLine(palindrome.Reverse("repaper"));

            //char[] chars = { 'a', 's', 'i', 'f', 'u', 'r', 'r', 'a', 'h' };

            //FirstDuplicate firstDuplicate = new FirstDuplicate();
            //Console.WriteLine(firstDuplicate.Run(chars));


            //Fibonacci f = new Fibonacci();
            //Console.WriteLine(f.Fibo(40));



            //int [] array1 = { 1, 2, 3 };
            //int[] array2 = { 10, 11, 12 };

            //CartesianProduct cp = new CartesianProduct();
            //int[,] arrays = cp.Test(array1, array2);
            //foreach (var item in arrays)
            //{
            //    Console.WriteLine(item);
            //}


            //Console.WriteLine(Pow.Recursion(2, 8));

            //Console.WriteLine(AvarageOfAscii.Avarage("GeeksforGeeks"));


            //var data = Number.DecimalToBinary(11);
            //foreach (var item in data)
            //{
            //    Console.WriteLine(item);
            //}

            //var reccounr = ArrayProblem.MaximumFrequency(arrs);
            //Console.WriteLine($"Element : {reccounr.element} and Times : {reccounr.count}");


            //var duplicate = DuplicateChar.Duplication("Programming");
            //foreach (var item in duplicate)
            //{
            //    if (item.Value > 1)
            //        Console.WriteLine($"Char : {item.Key} , count : {item.Value}");

            //}
            //int[] missing = { 1, 7, 9, 4, 5,6, 3,2 };

            //Console.WriteLine($"Missing Number : {ArrayProblem.GetSingleMissingNo(missing)}");

            //var missingNumbers = ArrayProblem.MissingNumbers(missing, 10);
            //foreach (var item in missingNumbers)
            //{
            //    Console.WriteLine($"missing Numbers : {item}");
            //}



            //var topTwo = ArrayProblem.FindTwoTop(arrs);
            //Console.WriteLine($"{topTwo.first} and {topTwo.second}");


            var data = ArrayProblem.PushAllZeroToTheEnd(1, 0, 2, 3, 0, 4, 0);

            for (int i = 0; i < data.Length; i++)
            {
                Console.WriteLine(data[i]);
            }

            int[] firstArray  = { 1, 2, 3, 4, 6 };
            int[] secondArray = { 3, 6, 7, 8 };
            ArrayProblem.FindCommon(firstArray, secondArray);
            Console.Read();
        }