美文网首页
ARTS打卡,第五周

ARTS打卡,第五周

作者: 没事举个栗子看看 | 来源:发表于2019-08-26 03:24 被阅读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.
  • 比较x === y,其中x和y是值,生成true或false。进行这种比较的方法如下:
  • 如果Type(x)和Type(y)不同,返回false
  • 如果Type(x)和Type(y)相同
    • 如果Type(x)是Undefined,返回true
    • 如果Type(x)是Null,返回true
    • 如果Type(x)是String,当且仅当x,y字符序列完全相同(长度相同,每个位置上的字符也相同)时返回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的数字值和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:

  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.
  • 比较x == y,其中x和y是值,生成true或false。进行这种比较的方法如下:
  • 如果Type(x)和Type(y)相同,返回x===y的结果
  • 如果Type(x)和Type(y)不同
    • 如果x是null,y是undefined,返回true
    • 如果x是undefined,y是null,返回true
    • 如果Type(x)是Number,Type(y)是String,返回 x==ToNumber(y) 的结果
    • 如果Type(x)是String,Type(y)是Number,返回 ToNumber(x)==y 的结果
    • 如果Type(x)是Boolean,返回 ToNumber(x)==y 的结果
    • 如果Type(y)是Boolean,返回 x==ToNumber(y) 的结果
    • 如果Type(x)是String或Number或Symbol中的一种并且Type(y)是Object,返回 x==ToPrimitive(y) 的结果
    • 如果Type(x)是Object并且Type(y)是String或Number或Symbol中的一种,返回 ToPrimitive(x)==y 的结果
    • 其他返回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

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

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

  • 对数据结构和算法建立一个较全面的认知体系
  • 掌握快速学习数据结构和算法的方法
  • 了解数据结构和算法的重要分类和经典题型

相关文章

  • ARTS打卡第五周

    ARTS打卡第五周 Algorithm:每周至少做一个 leetcode 的算法题 717. 单调数列 代码: }...

  • ARTS打卡,第五周

    每周完成一个ARTS:1.A(Algorithm)每周至少做一个 leetcode 的算法题2.R(Review)...

  • ARTS打卡第五周

    Tip: Algorithm: Share: Disruptor介绍及原理讲解 Review: dissectin...

  • 20181104_ARTS_W5

    第五周arts Algorithm-dp算法题 121. Best Time to Buy and Sell St...

  • ARTS第五周

    Algorithm leetCode 202 Happy Number将数字的每一个数字平方求和,如果等于1就是h...

  • 第五周ARTS

    Algorithmic LeetCode整数反转需要考虑多种情况,负数反转、数据溢出等等 https://leet...

  • ARTS第五周

    Algorithm。主要是为了编程训练和学习。每周至少做一个 leetcode 的算法题(先从Easy开始,然后再...

  • ARTS第五周2020620

    Algorithm 合并K个排序链表 合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。 示例...

  • ARTS挑战第五周

    Algorithm Review Tip 关于选择 面对多个选择,展望一下各个选择的最终结果,在结果上进行斟酌。 ...

  • ARTS打卡,第二周

    每周完成一个ARTS:1.A(Algorithm)每周至少做一个 leetcode 的算法题2.R(Review)...

网友评论

      本文标题:ARTS打卡,第五周

      本文链接:https://www.haomeiwen.com/subject/weaasctx.html