Home  >  Article  >  Web Front-end  >  Introducing js implementation solutions for some classic algorithms

Introducing js implementation solutions for some classic algorithms

jacklove
jackloveOriginal
2018-06-15 15:50:192463browse

Problem description
In a two-dimensional array, each row is sorted in increasing order from left to right, and each column is sorted in increasing order from top to bottom. Please complete a function, input such a two-dimensional array and an integer, and determine whether the array contains the integer.

function Find(target,array){
    //代码实现
    for(var i=0;i<array.length;i++){        for(var j=0;j<array[i].length;j++){            if(array[i][j] == target)                return true
        }
    }
}

Implementation idea: Since the matrix is ​​in increasing order horizontally and vertically, it can be traversed in a loop, first traversing each row, and then comparing the sub-elements of each row with the target target. If the array is a n*n two-dimensional array, the time complexity of this loop traversal is the square of n


Problem description
Please implement a function to replace the spaces in a string with " " . For example, when the string is We Are Happy, the replaced string is We Are Happy.

function replaceSpace(str){
    return str.split(" ").join("%20")
}

Implementation idea: split(array) can split a string, such as str="how are you", then split(array) is followed by how, are, and you (the default separator is,). Then array.join can merge arrays, such as arr = new Array[3], arr[0]="111", arr[1]="222", arr[2]="333", then arr.join(" !") followed by 111!222!333

This article introduces some js implementation solutions for classic algorithms. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:

javascript Set as homepage Add favorites JS code

Understanding of JS inheritance

JS decryption, online JS decryption decryption


The above is the detailed content of Introducing js implementation solutions for some classic algorithms. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn