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;
}
}
複製代碼
大前端視頻學習通道(點我)前端