zoukankan      html  css  js  c++  java
  • 撩课-Web大前端每天5道面试题-Day11

    1. 如何手写一个JQ插件?

    方式一:
    $.extend(src)  
    该方法就是将src合并到JQ的全局对象中去:
    $.extend({
          log: ()=>{alert('撩课itLike');}
    });
    
    方式二:
    $.fn.extend(src) 
    该方法将src合并到jquery的实例对象中去:
     $.fn.extend({
           log: ()=>{alert('撩课itLike');}
     });
    说说平衡二叉树?
    平衡二叉搜索树(Self-balancing binary search tree)
    又被称为AVL树。
    
    具有以下性质:
    
    1)它是一棵空树或它的左右两个子树
    的高度差的绝对值不超过1,
    并且左右两个子树都是一棵平衡二叉树。
    
    2)平衡二叉树必定是二叉搜索树,反之则不一定。
    
    3)平衡二叉树的常用实现方法有红黑树、AVL、
    替罪羊树、Treap、伸展树等。 
    
    最小二叉平衡树的节点的公式如下:
    F(n)=F(n-1)+F(n-2)+1 
    
    备注:
    1是根节点,
    F(n-1)是左子树的节点数量,
    F(n-2)是右子树的节点数量。

    3. 清除浮动和解决垂直外边距重叠的解决方案?

    问题描述:
    1) 父元素没有设置宽高,尺寸由子元素撑起;
    子元素一旦浮动,父元素高度会发生塌陷。
    2)子元素设置margin-top会作用的父元素的margin-top;
    此时会造成垂直外边距重叠。
    撩课小编:
    .clearfix::after,
    .clearfix::before{
          content: ' '; 
          display: table;
          clear: both; 
    }

    4. sessionStorage 、localStorage 和 cookie ?

    相同点:
    都用于浏览器端存储的缓存数据;
    
    不同点:
    1) 存储内容是否发送到服务器端
    
    当设置了Cookie后,数据会发送到服务器端,
    造成一定的宽带浪费;xxxstorage则会将数据保存
    到本地,不会造成宽带浪费;
    
    2) 数据存储大小不同
    
    Cookie数据不能超过4K,适用于会话标识;
    xxxstorage数据存储可以达到5M;
    
    3) 数据存储的有效期限不同
    
    cookie只在设置了Cookid过期时间
    之前一直有效,即使关闭窗口或者浏览器;
    
    sessionStorage,仅在关闭浏览器之前有效;
    localStorage,数据存储永久有效;
    
    4) 作用域不同
    cookie和localStorage是在同源同窗口中
    都是共享的;
    sessionStorage不在不同的浏览器窗口
    中共享,即使是同一个页面;

    5. 判断一个单词是否是回文?

    回文是指把相同的词汇或句子,
    在下文中调换位置或颠倒过来,
    产生首尾回环的情景,
    叫做回文,也叫回环。
    比如 cacac,redivider 。
    
    let checkPalindrom = (str)=>{  
        return str === 
        str.split('').reverse().join('');
    }

    6. 不借助临时变量,进行两个整数的交换?

    撩课小编:输入 a = 3, b =1, 输出 a = 1, b =3
    let swap = (a , b)=>{  
      b = b - a;
      a = a + b;
      b = a - b;
      return [a,b];
    }

    7. 运用JS 实现二叉查找树?

    二叉查找树,也称二叉搜索树、有序二叉树;
    是指一棵空树或者具有下列性质的二叉树:
    1) 任意节点的左子树不空,
    则左子树上所有结点的值均
    小于它的根结点的值;
    
    2) 任意节点的右子树不空,
    则右子树上所有结点的值
    均大于它的根结点的值;
    
    3) 任意节点的左、右子树
    也分别为二叉查找树;
    
    4) 没有键值相等的节点。
    
    5) 二叉查找树相比于其他数据结构
    的优势在于查找、插入的时间复杂度较低, 
    为O(log n)。
    
    二叉查找树是基础性数据结构,
    用于构建更为抽象的数据结构,
    如集合、multiset、关联数组等。
    
    实现:
    
    1)先设定好每个节点的数据结构
    class Node {  
      constructor(data, left, right) {
        this.data = data;
        this.left = left;
        this.right = right;
      }
    }
    
    2)树是由节点构成,由根节点逐渐延生到各个子节点,
    因此它具备基本的结构就是具备一个根节点,
    具备添加,查找和删除节点的方法。
    
    class BinarySearchTree  extend Node{
      constructor(data, left, right) {
          super(data, left, right);
          this.root = null;
      }
      insert(data) {
        let n = new Node(data, null, null);
        if (!this.root) {
          return this.root = n;
        }
        let currentNode = this.root;
        let parent = null;
        while (1) {
          parent = currentNode;
          if (data < currentNode.data) {
            currentNode = currentNode.left;
            if (currentNode === null) {
              parent.left = n;
              break;
            }
          } else {
            currentNode = currentNode.right;
            if (currentNode === null) {
              parent.right = n;
              break;
            }
          }
        }
      }
      remove(data) {
        this.root = this.removeNode(this.root, data)
      }
      removeNode(node, data) {
        if (node === null) {
          return null;
        }
        if (data === node.data) {
          if (node.left == null && node.right == null) {
            return null;
          }
          if (node.left === null) {
            return node.right;
          }
          if (node.right === null) {
            return node.left;
          }
          let getSmallest = (node) =>{
            if(node.left === null &&
               node.right == null) {
              return node;
            }
            if(node.left !== null) {
              return node.left;
            }
            if(node.right !== null) {
              return getSmallest(node.right);
            }
          }
          let temNode = getSmallest(node.right);
          node.data = temNode.data;
          node.right = this.removeNode(temNode.right,temNode.data);
          return node;
        } else if (data < node.data) {
          node.left = this.removeNode(node.left,data);
          return node;
        } else {
          node.right = this.removeNode(node.right,data);
          return node;
        }
      }
      find(data) {
        let current = this.root;
        while (current !== null) {
          if (data == current.data) {
            break;
          }
          if (data < current.data) {
            current = current.left;
          } else {
            current = current.right
          }
        }
        return current.data;
      }
    }
  • 相关阅读:
    P1456 Monkey King
    P3377 【模板】左偏树(可并堆)
    P1074 靶形数独
    P1120 小木棍
    P5490 【模板】扫描线
    糖糖别胡说,我真的不是签到题目
    最长公共子序列
    最长上升子序列
    数的三次方根
    地、颜色、魔法(dfs)
  • 原文地址:https://www.cnblogs.com/gxq666/p/10135721.html
Copyright © 2011-2022 走看看