Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2021-06-07 #9

Open
JunQu opened this issue Jun 10, 2021 · 4 comments
Open

2021-06-07 #9

JunQu opened this issue Jun 10, 2021 · 4 comments
Assignees
Labels
Milestone

Comments

@JunQu
Copy link
Owner

JunQu commented Jun 10, 2021

Algorithm

最大子列和(Maximum Subarray 53)

这个题目挺经典的,当然我第一次就没想出来,因为如果你要暴力法那么整个复杂度是O(n3)这是不能接受的。后来我发现我想的太多了,如果从整体来看这个题目就好弄的多,把之前的走过的当作一整个部分。
这个讲解推荐 《浙江大学数据结构》陈越老师的讲解,我很久之前听过这个,讲的让我感觉到对数据结构这门课程有了新的理解,推荐。

代码看起来就是非常简单的,其中的思想其实不是那么好懂,因为这是一个动态规划问题,如果你之前没有接触,那么你可能会被思维所限制。

const maxSubArray = function(nums) {
    let maxSum = nums[0];
    let subSum = 0;
    for(const num of nums) {
        subSum += num; // 得到前面部分的和
        if(subSum > maxSum) {
           maxSum = subSum
         }
        if(subSum < 0) {
            subSum = 0; // 小于0则影响和,舍弃
        }
    }
    return maxSum;
};
@JunQu
Copy link
Owner Author

JunQu commented Jun 10, 2021

English Review

@JunQu
Copy link
Owner Author

JunQu commented Jun 10, 2021

Tip

JS 判断类型的基本情况

虽然现在大家都推崇用 typescript ,但是仍旧有不少被 js 当中的类型所困惑的,最主要的原因是 js 是动态的,类型可以是被改变的,导致我们需要去检查类型。

数据类型(Data Types)

6种数据类型(2021年)分为undefined,boolean,number,string,bigInt,symbol。可以使用 typeof运算符:

typeof undefined === "undefined" // undefined 
typeof true === "boolean" // Boolean 
typeof 123 === "number" // Number
typeof "123" === "string" // String 
typeof 123n === "bigint" // BigInt 
typeof Symbol('foo') === "symbol" // Symbol

结构类型(Structural Types)

2种结构类型,分为 ObjectFunctionFunction依旧可以通过typeof 运算符进行判断:

typeof console.log === "function" // Function

结构元类型(Structural Root)

就是null,这里把它单独归为一类,是为了解释typeof instance === "object"很让人疑惑,它说如果 object 没有继承其他对象,那么它的值就是 null,null与上面的六种数据类型一起的七种称为原始类型(Primitive)。

null === null // null

现在基本讨论清楚基本类型的判断,现在还有一个 Object 该如何去处理。

Object

一般地,判断它是不是Object 也可以通过 typeof

object !== null && typeof object === "object"

这是满足最初的要求。通常我们更需要它更为具体,比如我需要一个日期对象Date,这样的判断只能让我知道它是Object, 无法让我知道它是一个 Date,这时候的我该如何判断?有人会说是通过 Object.prototype.toString.call方法,如下:

Object.prototype.toString.call(new Date()) // "[object Date]"

好像是可以,这样的区分普通对象和 Date对象,但是当我有这样的普通对象:

const fakeDate = {};
fakeDate[Symbol.toStringTag] = "Date"

然后我们就不太能区分了:

Object.prototype.toString.call(new Date()) // "[object Date]"
Object.prototype.toString.call(fakeDate) // "[object Date]"

甚至可以被修改:

const date = new Date()
Object.prototype.toString.call(date) // "[object Date]"
date[Symbol.toStringTag]="FakeDate"
Object.prototype.toString.call(date) // "[object FakeDate]"

这样显得Object.prototype.toString.call很没面子😞。那么我们有没有更好的方法呢,答案是使用 typescript (逃。
toStringTag出来以后,这个方法就不太可靠了。我想目前Object.prototype.toString.call 还是可以使用的,但是不能去信任它了,把类型判断这样的费力不讨好的麻烦事交给专业的 typescript , 而我们更应该在 typescript 做类型体操 (逃。

好吧,判断基本类型其实也差不多了,还有类似判断它是不是一个整数(isInteger)之类的也有点麻烦,我主要想记录 Object.prototype.toString.call 并不可靠了,因为只要规范是通过这个判断类型的,但是实际上是不能被完全信任了。

@JunQu
Copy link
Owner Author

JunQu commented Jun 10, 2021

Share

好玩

工具使用

  • 我是坚定的 jetbrain 用户,当然用什么工具只是方便,但是现在吹 vscode 的人实在太多太多了,我必须也要分享一下 webstorm : https://zhuanlan.zhihu.com/p/373510139

端午节

端午快乐!记录一下,许哥给我们的端午粽子。(PS去年中秋也是这个牌子,好像不是很好吃🤐,可能是金钱的味道吧)
IMG_2089

@JunQu
Copy link
Owner Author

JunQu commented Jun 10, 2021

学而不思则罔,思而不学则殆

@JunQu JunQu self-assigned this Jun 11, 2021
@JunQu JunQu added the ARTS label Jun 11, 2021
@JunQu JunQu added this to the 2021-06 milestone Jun 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant