ARTS打卡,第五周

2019-08-26  本文已影响0人  没事举个栗子看看

每周完成一个ARTS:
1.A(Algorithm)每周至少做一个 leetcode 的算法题
2.R(Review)阅读并点评至少一篇英文技术文章
3.T(Tip)学习至少一个技术技巧
4.S(Share)分享一篇有观点和思考的技术文章


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中"=="与"==="的区别

通过查阅ECMAScript® 2016 Language Specification,得出了以下的结论:
=== 严格相等,会比较两个值的类型和值
== 抽象相等, 比较时,会先进行类型转换,然后再比较值
具体怎么转换,左边转换成右边类型还是右边转换成左边类型?

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:

  1. If Type(x) is different from Type(y), return false.
  2. 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 +0 and y is ‐0,return true.
    • e. If x is ‐0 and y is +0, return true.
    • f. Return false.
  3. Return SameValueNonNumber(x, y).
    NOTE This algorithm differs from the SameValue Algorithm in its treatment of signed zeroes and NaNs.
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:

  1. If Type(x) is the same as Type(y), then
    a. Return the result of performing Strict Equality Comparison x === y.
  2. If x is null and y is undefined, return true.
  3. If x is undefined and y is null, return true.
  4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
  5. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
  6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
  7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
  8. If Type(x) is either String, Number, or Symbol and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
  9. If Type(x) is Object and Type(y) is either String, Number, or Symbol, return the result of the comparison ToPrimitive(x)== y.
  10. Return false.

具体SameValueNonNumber()和ToPrimitive()两个操作,可以自己查阅,就不再这里CTRL+V了。
我们接下来按照文档说明看几个神奇的例子

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,我靠!![]首先要转换Boolean,然后再取反,Boolean([])是true,取反是false,所以成立。惊然发现[] ==![],有点神奇,有点蒙蔽,哈哈
console.log(null == false);
//运算规则的最后一条,前面所有的都不满足,最后返回false,因为按照最后一条走的,所以null == true也是false

S

前端该如何准备数据结构和算法?

这篇文章分析的角度是从为什么、怎么做、做什么的角度对数据结构和算法进行的全面分析(针对前端角度),对自己的帮助呢,可能有以下几个点:

上一篇下一篇

猜你喜欢

热点阅读