1. Basic details of the data structure based on practical
Data structure (data structure [strʌktʃə (r)]) is a computer store, organize data;
(Algorithm [ælɡərɪðəm]) algorithm is a solution to the problem / steps and strategies;
Data structure Array
Js array structure is very simple (browser help us deal with a good package)
- You can store different types of data values
- An array of storage capacity along with automatic scaling content
- Provides a method of operating the array Array.prototype
Advantage : based on the index to find and get a direct, high efficiency;
Drawbacks : The intermediate insert and delete, performance is very low ( an array of collapse and delete optimization of intermediate terms)
Stack stack structure
- Last out;
- Only some operations: adding comprises (push) and delete (the stack);
- Recursive algorithm infinite recursion stack overflow occurs.
Stack method:
class Stack { container = []; // 进栈 enter(element) { this.container.unshift(element); } // 出栈 leave() { return this.container.shift(); } // stack length size () { return the this .container.length; } // get results stack value () { return the this .container.slice (0 ); } } let sk = new Stack;
Interview a:
There are six elements, in the order of 654 321 into the stack (center can always pop), which asked the stack is not legal :( c: After following the principles advanced out )
A: 5 4 3 6 1 2
B: 4 5 3 2 1 6
C: 3 4 6 5 2 1
D: 2 3 4 1 5 6
Interview II: decimal binary transfer
For example: 58 => 111010
/* * Ideas: the decimal number is divisible by 2 and with (because the binary is full 2 into 1) * Get the number of the remaining n% 2 * To obtain its quotient n / 2 (when the value 0 is divisible stop) * Quotient continue to spend time divided by two until the quotient is 0 so far; all the remainder from the tail to the top of the order on the link; */ // Method NumberObject.toString. 1 (the radix) var NUM = 58 ; num.toString(2); //方法2 Numer.protype.decimal2binary = function decimal2binary(){ SK the let = new new Stack, decimalNum = the this .valueOf (); IF (decimalNum == 0) return '0' // negative 0, since 0 is converted to a binary 0 the while (decimalNum> 0 ) { sk.enter(decimalNum%2); decimalNum = Math.floor(decimalNum/2); } return sk.value().join('') } console.log((10).decimal2binary() );
Queue queue structure
- FIFO
- Delete allows the front end, a rear end allowing the insertion;
- Special: Priority Queue
class Queue { container = []; // 进入 enter(element) { this.container.push(element); } // 离开 leave() { return this.container.shift(); } // length of the queue size () { return the this .container.length; } // Get result queue value () { return the this .container.slice (0 ); } }
Interview questions: flower drum transfer
N individuals play games together, form a circle, from 1 count start, count to M people automatically eliminated; the last remaining person will win, the last remaining ask which one is the original?
function game(n, m) { let qe = new Queue; for (let i = 0; i < n; i++) { qe.enter(i + 1); } the while (qe.size ()>. 1) { // when only one loop terminates for (the let I = 0; I <m -. 1; I ++) { // not chosen one re-added to the queue qe .enter (qe.leave ()); } qe.leave(); } return qe.value().toString(); } console.log(game(8, 5)); //3