大三末之初级前端面试之上面 问题答案(三)

前三题都用的typescript实现

(一)从 多个网页爬取去内容然后进行解析

有10个网页,1.txt 到 10.txt

http://dy-public.oss-cn-shenzhen.aliyuncs.com/interviewTestData/1.txt

<abc> <de
f>data : 1992; name : agent 1 def>abc>

0.格式有可能有区别
1.分别实现同步,异步的爬取方式
2.网页下载使用nodejs的http即可
3.使用正则表达式获取data后面的字段,将10个文件的data值全部获取,并相加

我的答案

api.ts


import superagent = require('superagent');
import cheerio = require('cheerio');

export const remote_get = function(url: string) {

    const promise = new Promise(function (resolve, reject) {
        superagent.get(url)
            .end(function (err, res) {
                if (!err) {
                    resolve(res);
                } else {
                    console.log(err)
                    reject(err);
                }
            });
    });
    return promise;
}

async.ts

import superagent = require('superagent');
import cheerio = require('cheerio');
const remote_get =function(url: string) {
    const promise = new Promise(function (resolve, reject) {
        superagent.get(url)
            .end(function (err, res) {
                if (!err) {
                    resolve(res);
                } else {
                    console.log(err)
                    reject(err);
                }
            });
    });
    return promise;
}
let result = 0;
let count = 11;
let sum=0;
/**
 * 
 * @param nums 
 * @param hasEnd 
 */
async function crawl(){
 for(let nums=1;nums<11;nums++){
        let res = await remote_get('http://dy-public.oss-cn-shenzhen.aliyuncs.com/interviewTestData/'+nums+'.txt');
        var zzz=res.text;
        zzz = zzz.replace(/\s/g,"");
        let bluefile = zzz.match(/\d+(\\.\\d+){0}/);
        if(bluefile!=null){
            result+=parseInt(bluefile[0]);
        }  
    }
    console.log("最後結果"+result);
}
crawl();

实现一个quicksort算法

kp.ts

function quick( arr: number[],left:number,right:number): Array<number>{
    var f, t;
     var rtemp, ltemp;
       rtemp = right;
        ltemp = left;
       f = arr[(right + left) / 2];// 分解值
        while (ltemp < rtemp) {
            while (arr[ltemp] < f) {
                ++ltemp;
            }
            while (arr[rtemp] > f) {
                --rtemp;
            }
            if (ltemp <= rtemp) {
                t = arr[ltemp];
                arr[ltemp] = arr[rtemp];
                arr[rtemp] = t;
                --rtemp;
                ++ltemp;

            }
            if (ltemp == rtemp) {
                ltemp++;
            }
            if (left < rtemp) {
                quick(arr, left, ltemp - 1);// 递归 调用
            }
            if (ltemp < right) {
                quick(arr, rtemp + 1, right);
            }
        }
        return arr;
    }

 function sort(arr: number[], begin: number, end: number): Array<number> {
    if (end <= begin)
        return arr;
    let i = begin;
    let j = end;
    let key = arr[begin];
    while (true) {
        while (true) {
            if (i == j) break;
            if (arr[j] < key) {
                let temp = arr[j];
                arr[j] = arr[i];
                arr[i] = temp;
                break;
            }
            j--;
        }
        while (true) {
            if (i == j) break;
            if (arr[i] > key) {
                let temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
                break;
            }
            i++;
        }
        if (i == j)
            break;
    }
    if (end - j > 1) {
        arr = sort(arr, j + 1, end);
    }
    if (i - begin > 1) {
        arr = sort(arr, begin, i);
    }
    return arr;
}
console.log(sort([1,5,3,4,2,4,3,5],0,7));

3. 实现一个二叉树,使插入、查询、删除的时间复杂度为O(logn)

class Tree {
// implement your code
}

let t = new Tree()
t.add(2)
t.hasValue(2)  // true
t.add(4)
t.add(4)
t.add(3)
t.print()      // 升序输出,[2, 3, 4]
t.remove(2)    // [3, 4]

twotree.ts

//实现一个二叉树,使插入、查询、删除的时间复杂度为O(logn) 
//class Tree {
//     // implement your code
//     }

//     let t = new Tree()
//     t.add(2)
//     t.hasValue(2)  // true
//     t.add(4)
//     t.add(4)
//     t.add(3)
//     t.print()      // 升序输出,[2, 3, 4]
//     t.remove(2)    // [3, 4]
let Tree = (function() {
    class Node {
        key:any;
        left:any;
        right:any;
        constructor(thekey:any) {
            this.key = thekey;
            this.left = null;
            this.right = null;
        }
    }
    class BinarySearchTree {
        root:any;
        constructor() {
            this.root = null;
        }
            /**
             * 插入:向二叉树插入一个新的键
             * @param key 
             */
        add(key:any) {
            var newNode = new Node(key);
            if(this.root === null) {
                this.root = newNode;
            } else {
                this.insertNode(this.root, newNode);
            }
        }
        insertNode(node:any, newNode:any) {
            if(newNode.key < node.key) {
                if(node.left === null) {
                    node.left = newNode;
                } else {
                    this.insertNode(node.left, newNode);
                }
            } else {
                if(node.right === null) {
                    node.right = newNode;
                } else {
                    this.insertNode(node.right, newNode);
                }
            }
        }
        /**
         * 查询:在二叉树查找一个键,如果节点存在,则返回true;如果不存在,则返回false。
         * @param key 
         */
        hasValue(key:number) {
            console.log(this.searchNode(this.root, key))
            return this.searchNode(this.root, key);
        }
        searchNode(node:any, key:any):any {
            if(node === null) {
                return false;
            }

            if(key < node.key) {
                return this.searchNode(node.left, key);

            } else if(key > node.key) {
                return this.searchNode(node.right, key);

            } else {
                return true;
            }
            return 0;
        }
        /**
         * 升序输出
         * @param callback 
         */
        print(callback:any) {
            this.printNode(this.root, callback);
        }

        printNode(node:any, callback:any) {
            if(node !== null) {
                this.printNode(node.left, callback);
                callback(node.key);
                this.printNode(node.right, callback);
            }
        }
        min(){
            return this.minNode(this.root);
        }

        minNode(node:any) {
            if(node) {
                while(node && node.left !== null) {
                    node = node.left;
                }

                return node.key;
            }
            return null;
        }
        /**
         * 删除:从树中移除某个键
         */

        remove(element:number) {
            if(this.hasValue(element)){
                this.root = this.removeNode(this.root, element);
                console.log("已经删除节点:"+element);
            }else{
                console.log("不存在节点:"+element+'请从新输入');
            }




        }
        findMinNode(node:any) {
            while(node && node.left !== null) {
                node = node.left;
            }

            return node;
        }

        removeNode(node:any, element:number) {
            if(node === null) {
                return null;
            }
            if(element < node.key) {
                node.left = this.removeNode(node.left, element);
                return node;

            } else if(element > node.key) {
                node.right = this.removeNode(node.right, element);
                return node;

            } else { 

                if(node.left === null && node.right === null) {
                    node = null;
                    return node;
                }
                if(node.left === null) {
                    node = node.right;
                    return node;

                } else if(node.right === null) {
                    node = node.left;
                    return node;
                }
                var aux = this.findMinNode(node.right);
                node.key = aux.key;
                node.right = this.removeNode(node.right, aux.key);
                return node;
            }
        };

    }
    return BinarySearchTree;
})()
var tree = new Tree();
let arrss:any=[];
tree.add(11);
tree.add(6);
tree.add(13);
tree.add(5);
tree.add(3);
function printNodes(value:any){
    arrss.push(value)
        console.log(arrss);
}
tree.print(printNodes);
tree.remove(1);
tree.remove(5);
tree.hasValue(11);

真实还原github搜索页面,即ui相同
调用github api实现搜索功能
使用 vue

你可能感兴趣的:(面试合集(有讲解))