Stacks, queues algorithm problem

  • Stack queue, the completion queue Push and Pop operations. Queue element is of type int

// idea 

stack from the stack can only access, the head of the queue from the queue, into the tail, by means of two stacks: stack top of the tail 1, the top of the stack 2 for the head of the queue 
push pressed into an element from the tail, s1 from the press-fitting, and non-empty case stack is empty as no return value 
pop pop-up element from a head of the queue, when the head of the queue is not empty s2 s2 at the top of the stack, the head of the queue empty s2 s1 bottom of the stack, the need to first s1 All the elements are sequentially inject s2, s2 then pops the stack 
when all elements from a pop-up note pressed into B, a because the array changes, it is necessary to keep down the length of the array first, then use the value stored for the control loop 
if s1 is empty, queue is empty, pop pop null 

var Stack1 = [],
   // var Stack1 = [], 
  stack2 divided by 2. = []; // define the two stacks 
// enqueued modify the original array, no return value 
function Push (Node) {
   // Write code here Wallpaper 
  stack1.push (Node); // onto the stack 1 
}
 // dequeued modify the original list, returns the elements dequeue 
function POP () {
   // Write code here Wallpaper 
  //2 the stack is not empty, return stack top element 2 
  IF (stack2.length == 0!) Return stack2.pop ();
   // stack 2 empty, return stack stack bottom element 1 
  IF (stack2.length === 0 ) {
     // stack 1 empty, return empty 
    IF (stack1.length === 0) return  null ;
     // stack 1 is not empty, all the stack elements 1 placed on the stack 2, and then returns the top element 
    let len = stack1.length ; // for stack1 modified loop array, the first loop counter as stored len 
    for (the let I = 0; I <len; I ++ ) { 
      stack2.push (stack1.pop ()); 
    } 
    return stack2.pop ( ); 
  } 
}
  • Stack achieved with queue

// queue: FIFO. Simply put, there is an array () advancing each element by push, launched the first element by shift (), this form is the queue. 
// stack: last out. Simply put, there is an array () advancing each element by push, launched last element by pop (), this form is the stack. 
If little friends still do not understand, then we talk about two examples: 

Queue: canteen Dafan. We need to line up, first to the front row, so after a meal can go first. 
Stack; matryoshka, or pyramid. Finally the advanced needs, or else not get out, or Rohan directly down. 
Finally, according to this idea, we can solve easily. 

var MyStack = function () {
   the this .stask = []; 
}; 

MyStack.prototype.push = function (X) {
   the this .stask [ the this .stask.length] = X; 
}; 

MyStack.prototype.pop = function ( ) { 
  the let ARR = the this.stask;
  let last = this.stask.length - 1;
  let temp = arr[last];
  this.stask.length = this.stask.length - 1;
  return temp;
};

MyStack.prototype.top = function() {
  let arr = this.stask;
  let last = this.stask.length - 1;
  return arr[last];
};

MyStack.prototype.empty = function() {
  let arr = this.stask;
  if (!arr.length) {
    return true;
  } else {
    return false;
  }
};

 

  •  Stack data structure definition, implement this type can be a min function smallest elements contained in the stack, pop (), push () , time complexity should be O(1).

// thinking 

time complexity should be O ( . 1 ) so that the minimum value to be saved after each stack to push elements, a new stkMin, and stk synchronization 
push-- first element is a stack, the stack is the current minimum element when, stkMin pressed into the node, or the top element is pressed into stkMin (minimum value is a minimum value still before) 
POP - pop stkMin synchronization and stk 
min-- return stkMin top element 


var stk = [];
 var stkMin = [];
 function Push (Node) {
   // Write code here Wallpaper 
  stk.push (Node); 
  stkMin.length === Node 0 || <stkMin [stkMin.length -. 1 ]
? stkMin.push (Node) // stkMin empty / node is less than the top element, it is the minimum node
: stkMin.push (stkMin [stkMin.length -. 1]); // when the node is greater than the top element, the top element is the minimum value } function POP () { // write code here if (stk.length === 0) return null; stkMin.pop(); return stk.pop(); } function top() { // write code here return stk[stk.length - 1]; //返回栈顶元素 } function min() { // write code here return stkMin[stkMin.length - 1]; }
  • Two input sequence of integers, the first sequence representing a pressed stack order, determines whether it is possible for the second sequence the order of the pop-up stack. All figures are not pushed onto the stack is assumed equal.

// idea 

to judge a stack of pop-up sequence is not the sequence, in fact, is to determine the sequence 1 pushed onto the stack, the top element may be formed if the sequence 2, the top element, there are three possibilities:
 1 . The current top of the stack
 2 . Pop stack the new top of the stack after
 3 continue to press into the new top of the stack 
comparison order: 1,2,3 , can pop up all elements 
by means of an auxiliary stack, followed by all the elements that pushed the stack, set a pointer pointing to the need idx pop-up elements, the first element in the sequence of the initial pop-up point 
after each press-fit, are more idx element and the top element are equal, if equal, to eject the value, the next point to a idx, cycle compare all the values are all equal popup until the two values are unequal, no pop-up sequence, continue to press the 
subsequent array elements are all push to complete, if there is no auxiliary stack pop elements, represents 
not the pop-up sequence, if the stack is empty, on behalf of a pop-up sequence. 

function IsPopOrder (pushV, popV) {
   // Write code here Wallpaper 
  // any empty array / arrays of unequal length, the condition is not satisfied 
  IF (pushV.length popV.length === === 0 || 0 || popV.length ! == pushV.length)
     return  false ;
   //Single array, a comparator element 
  IF (pushV.length && popV.length === ===. 1. 1) return pushV [0] === popV [0 ]; 
  the let STK = []; // auxiliary stack 
  let idx = 0; // pointer pointing to the first element of popV 
  // will pushV pressed into the auxiliary stack 
  for (the let I = 0; I <pushV.length; I ++ ) { 
    stk.push (pushV [I]); 
    // STK popV empty or top element and the pointer element are not equal, the cycle skip, continue stack 
    // equal elements and the stack pointer popV elements, circulation and pop the top element in the comparison pointer element 
    the while (stk.length ! = 0 && STK [stk.length -. 1] === popV [IDX]) { 
      stk.pop (); // the top element from the stack 
      IDX ++; // pointer down, continue comparing 
    } 
  }
  return stk.length === 0; // popV all of the elements in the stack are in order, the sequence is a pop 
} 
IsPopOrder ([ . 1, 2,. 3,. 4,. 5], [. 4,. 5,. 3, 2 ,. 1 ]); 
IsPopOrder ([ . 1], [2]);

 

Guess you like

Origin www.cnblogs.com/huahongcui/p/11519622.html