每周完成一個(gè)ARTS:
1.A(Algorithm)每周至少做一個(gè) leetcode 的算法題
2.R(Review)閱讀并點(diǎn)評(píng)至少一篇英文技術(shù)文章
3.T(Tip)學(xué)習(xí)至少一個(gè)技術(shù)技巧
4.S(Share)分享一篇有觀點(diǎn)和思考的技術(shù)文章
A
Longest Palindromic Substring
/**
* @param {string} s
* @return {string}
*/
var longestPalindrome = function(s) {
if (s === '')
return s
let maxStr = s[0]
let len = s.length
for (let i = 0; i < len - 1; i++) {
let tempS = s[i]
let k = 1
let cutBol = true
for (let j = i + 1; j < len; j++) {
if (s[i] === s[j] && cutBol) {
tempS += s[j]
continue
}
if (i - k < 0) {
break;
}
if (s[j] === s[i - k]) {
cutBol = false
tempS = s[i - k] + tempS + s[j]
k++
} else {
break;
}
}
maxStr = tempS.length > maxStr.length ? tempS : maxStr
}
return maxStr
};
console.log(longestPalindrome('ababd'))
R&T
js中"=="與"==="的區(qū)別
通過(guò)查閱ECMAScript? 2016 Language Specification,得出了以下的結(jié)論:
=== 嚴(yán)格相等,會(huì)比較兩個(gè)值的類型和值
== 抽象相等, 比較時(shí),會(huì)先進(jìn)行類型轉(zhuǎn)換,然后再比較值
具體怎么轉(zhuǎn)換,左邊轉(zhuǎn)換成右邊類型還是右邊轉(zhuǎn)換成左邊類型?
7.2.14 Strict Equality Comparison
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:
- If Type(x) is different from Type(y), return false.
- If Type(x) is Number, then
- a. If x is NaN, return false.
- b. If y is NaN, return false.
- c. If x is the same Number value as y, return true.
- d. If x is
+0and y is‐0,return true.- e. If x is ‐0 and y is +0, return true.
- f. Return false.
- Return SameValueNonNumber(x, y).
NOTE This algorithm differs from the SameValue Algorithm in its treatment of signed zeroes and NaNs.
- 比較x === y,其中x和y是值,生成true或false。進(jìn)行這種比較的方法如下:
- 如果Type(x)和Type(y)不同,返回false
- 如果Type(x)和Type(y)相同
- 如果Type(x)是Undefined,返回true
- 如果Type(x)是Null,返回true
- 如果Type(x)是String,當(dāng)且僅當(dāng)x,y字符序列完全相同(長(zhǎng)度相同,每個(gè)位置上的字符也相同)時(shí)返回true,否則返回false
- 如果Type(x)是Boolean,如果x,y都是true或x,y都是false返回true,否則返回false
- 如果Type(x)是Symbol,如果x,y是相同的Symbol值,返回true,否則返回false
- 如果Type(x)是Number類型
- 如果x是NaN,返回false
- 如果y是NaN,返回false
- 如果x的數(shù)字值和y相等,返回true
- 如果x是+0,y是-0,返回true
- 如果x是-0,y是+0,返回true
- 其他返回false
7.2.13Abstract Equality Comparison
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
- If Type(x) is the same as Type(y), then
a. Return the result of performing Strict Equality Comparison x === y.- If x is null and y is undefined, return true.
- If x is undefined and y is null, return true.
- If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
- If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
- If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
- If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
- If Type(x) is either String, Number, or Symbol and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
- If Type(x) is Object and Type(y) is either String, Number, or Symbol, return the result of the comparison ToPrimitive(x)== y.
- Return false.
- 比較x == y,其中x和y是值,生成true或false。進(jìn)行這種比較的方法如下:
- 如果Type(x)和Type(y)相同,返回x===y的結(jié)果
- 如果Type(x)和Type(y)不同
- 如果x是null,y是undefined,返回true
- 如果x是undefined,y是null,返回true
- 如果Type(x)是Number,Type(y)是String,返回 x==ToNumber(y) 的結(jié)果
- 如果Type(x)是String,Type(y)是Number,返回 ToNumber(x)==y 的結(jié)果
- 如果Type(x)是Boolean,返回 ToNumber(x)==y 的結(jié)果
- 如果Type(y)是Boolean,返回 x==ToNumber(y) 的結(jié)果
- 如果Type(x)是String或Number或Symbol中的一種并且Type(y)是Object,返回 x==ToPrimitive(y) 的結(jié)果
- 如果Type(x)是Object并且Type(y)是String或Number或Symbol中的一種,返回 ToPrimitive(x)==y 的結(jié)果
- 其他返回false
具體SameValueNonNumber()和ToPrimitive()兩個(gè)操作,可以自己查閱,就不再這里CTRL+V了。
我們接下來(lái)按照文檔說(shuō)明看幾個(gè)神奇的例子
console.log([] == 0);
//ToPrimitive([])==‘ ’而 ToNumber(‘ ’)==0 因此[]=0 返回true
console.log(true == 1);
// true是Boolean,ToNumber(true)==1 返回true
console.log([] == false);
//false是Boolean,我們要比較 []==ToNumber(false)即 []==0,返回 true
console.log(![] == false);
//按照上面的例子,按照以往的思維,一定返回false,但真的返回了true,我靠!![]首先要轉(zhuǎn)換Boolean,然后再取反,Boolean([])是true,取反是false,所以成立。驚然發(fā)現(xiàn)[] ==![],有點(diǎn)神奇,有點(diǎn)蒙蔽,哈哈
console.log(null == false);
//運(yùn)算規(guī)則的最后一條,前面所有的都不滿足,最后返回false,因?yàn)榘凑兆詈笠粭l走的,所以null == true也是false
S
前端該如何準(zhǔn)備數(shù)據(jù)結(jié)構(gòu)和算法?
這篇文章分析的角度是從為什么、怎么做、做什么的角度對(duì)數(shù)據(jù)結(jié)構(gòu)和算法進(jìn)行的全面分析(針對(duì)前端角度),對(duì)自己的幫助呢,可能有以下幾個(gè)點(diǎn):
- 對(duì)數(shù)據(jù)結(jié)構(gòu)和算法建立一個(gè)較全面的認(rèn)知體系
- 掌握快速學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)和算法的方法
- 了解數(shù)據(jù)結(jié)構(gòu)和算法的重要分類和經(jīng)典題型