Here you only need to be able to achieve it, not how good the performance is

Fibonacci numbers

function Fibonacci(n)
{
    if(n<0) {return;
    }
    else if(n == 0) {
        return 0;
    }else if(n == 1 || n == 2) {return 1;
    }else{
        return Fibonacci(n-1) + Fibonacci(n-2); }}Copy the code

Just note here:

  1. Recursive exit;
  2. And they want to know that the 0th term is 0, and the first term is 1

Queue implementation with two stacks (unknown)

Use two stacks to implement a queue, which performs Push and Pop operations. Elements in the queue are of type int.

let stack1=[],
    stack2=[];// Help
function push(node)
{
    stack1.push(node);
}
function pop()
{
    if(stack2.length===0) {while(stack1.length){ stack2.push(stack1.pop()); }}return stack2.pop();
}

Copy the code

Easy way: One stack

var stack1 = [];
var stack2 = [];
function push(node) {
    stack1.push(node);
}
function pop()
{
    return stack1.shift()
}
Copy the code

A string representing a numeric value

Implement a function to determine whether a string represents a numeric value (both integer and decimal). For example, the string “+ 100”, “5 e2”, “123”, “3.1416” and “1 e – 16” numerical value. But “12e”,” 1A3.14 “,”1.2.3″,”+-5” and “12E +4.3” are not.

/** * The class name, method name, and parameter name have been specified, do not modify, directly return method specified value ** *@param STR string String *@return Bool Bool */
function isNumeric( str ) {
    let val = Number(str);
    if( Number.isNaN(val)) 
         return false;
    return true
}
module.exports = {
    isNumeric : isNumeric
};
Copy the code

Duplicate numbers in an array

All the numbers in an array of length N are in the range 0 to n minus 1. Some numbers in the array are duplicated, but it is not known how many are duplicated. I don’t know how many times I repeat each number. Please find any duplicate number in the array. For example, if you input an array of length 7 [2,3,1,0,2,5,3], the corresponding output would be 2 or 3. Output -1 if there is an invalid input

/** * The class name, method name, and parameter name have been specified, do not modify, directly return method specified value ** *@param Numbers int One-dimensional array *@return Int * / integer
function duplicate( numbers ) {
    // write code here
    let n = numbers.length;
    for(let i = 0; i < n; i++) {
      while(numbers[i] ! == i) {if (numbers[i] === numbers[numbers[i]]) return numbers[i];
            consttemp = numbers[numbers[i]]; numbers[numbers[i]] = numbers[i]; numbers[i] = temp; }}return -1;
}
module.exports = {
    duplicate : duplicate
};
Copy the code