We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Stack<int>aStack=newStack<int>();Stack<int>bStack=newStack<int>();// Number of queriesintq=Convert.ToInt32(Console.ReadLine());for(inti=0;i<q;i++){string[]line_temp=Console.ReadLine().Split(' ');int[]line=Array.ConvertAll(line_temp,Int32.Parse);if(bStack.Count==0){while(aStack.Count!=0)bStack.Push(aStack.Pop());}// Enqueueif(line.Length==2){if(bStack.Count==0)bStack.Push(line[1]);elseaStack.Push(line[1]);}else{// Dequeueif(line[0]==2)bStack.Pop();// Printelseif(line[0]==3)Console.WriteLine(bStack.Peek());}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Queue using Two Stacks
You are viewing a single comment's thread. Return to all comments →
My C# implementation: