js- wins the title records offer brush (completeness code)

1. Positive values ​​of several square

Given a double base type and floating-point type int integer exponent. Seeking exponent of the power base.

Ensure the base and exponent are not simultaneously 0

My solution

Math.pow(x,y)??
function Power(base, exponent)
{
        let r = 1
        while(exponent--){
            r*=base
        }
        return r
}

Other analysis

The above approach will not do if you see a problem understanding. Exponentiation power can play a flower. . .
Fast power
Baidu Encyclopedia described more intuitive interface, as shown below
Here Insert Picture Description
that is a calculated power of 11, as long as the computing power of a 1, a power of 2 and a power of 8, and then multiplying the line. We need to find the binary representation of the exponents of the bits 1
process

  • Exponent number is shifted to 0, the cycle is stopped

    • When exponent & 1 == 0the time,res = res*x
    • x = x*x
    • Number of exponet right one

    Here Insert Picture Description

function Power(base, exponent)
{
    let res = 1
    let n
    if(exponent==0){
        return 1
    }else if(exponent<0){   //指数为负
        n = -exponent
    }else{
        n = exponent
    }
    
    while(n){
        if(n&1){
            res *=base 
        }
        base *=base  //这里注意!!!
        n = n >>1
    }
    return exponent>0 ? res:1/res
}

2. Adjust the order of the array so that in front of the even-odd

Enter an array of integers, to realize a function to adjust the order of the numbers in the array, such that all the odd part of the front half of the array, is located in the second half of all the even array, and between odd and ensure a relatively even, odd and even the same position.

My solution

Initial idea: iterate, then encountered an even number will be deleted from the array into the end of the push.
but! If the end of the array is odd, i.e. not finished through the array, all even have push into the end, followed by an even cycle destroys the sequence.

function reOrderArray(array)  //错错错
{
    for(let i = 0;i<array.length;i++){
        if(array[i]%2 ==0){
            let tmp = array[i]
            array.splice(i,1)
            array.push(tmp)
        }
    }
    return array
}

Want a way: iterate the even number into an empty array, then an array of empty array with the original stitching.
but! We met consecutive even when deleted even-even behind the top front, should remain in this one deal, but i ++, and on an even skipped

function reOrderArray(array) //错错错
{
    let arr = []
    for(let i = 0;i<array.length;i++){  //循环出问题了
        if(array[i]%2 ==0){
            let tmp = array[i]
            array.splice(i,1)
            arr.push(tmp)
        }
    }
    return array.concat(arr)
}

The ultimate awkward: two empty array, an odd place, a place even number. After traversing over, splicing the two arrays.

 function reOrderArray(array)  //对了
{
    let arr1 = []
    let arr2 = []
    for(let i = 0;i<array.length;i++){
        if(array[i]%2 ==0){
            arr2.push(array[i])
        }else{
            arr1.push(array[i])
        }
    }
    return arr1.concat(arr2)
}

Actually, the reason is that the problem before the two approaches, array traversal when it changed the original array, bit by bit to traverse when an error occurs.

Published 21 original articles · won praise 0 · Views 177

Guess you like

Origin blog.csdn.net/adrenalineiszzz/article/details/104842552
Recommended