铃予

文章 评论 浏览 29

铃予 2022-05-04 13:55:19
const sevenTimes = num => eval(new Array(7).fill(num).join('+'))

sevenTimes(10) // 70
  • 加号字符串不算加号运算符,那我是不是可以。。。eval(${num}*7)

第 102 题:不用加减乘除运算符,求整数的 7 倍

铃予 2022-05-04 13:53:56

因为父容器的宽度是600px,大于left跟right容器的宽度和,余100px,
没有margin跟padding影响,然后flex-grow的比是1:2,
所以left会多100 / 3 * 1 = 33.3的宽度,right会多100 / 3 * 2 = 66.7
最终left宽300 + 33.3 = 333.3,right宽200 + 66.7 = 266.7

第 156 题:求最终 left、right 的宽度(变形)

铃予 2022-05-04 05:28:42

链表是一种物理存储单元上非连续、非顺序的存储结构,它既可以表示线性结构,也可以用于表示非线性结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。

使用链表结构可以克服数组链表需要预先知道数据大小的缺点,链表结构可以充分利用计算机内存空间,实现灵活的内存动态管理。

3.3 链表

3.3.1 单向链表

链表的最简单形式是单向链表,它的每个节点包含2部分:数据+指向下一节点的指针。最后一个节点指向NULL。

JavaScript实现

class Node {
    constructor(element) {
        this.element = element
        this.next = null
    }
}
class LinkedList {
    constructor() {
        this.head = null
        this.length = 0
    }
    append(el) {
        const node = new Node(el)
        if (!this.head) {
            this.head = node
        } else {
            let current = this.head
            while (current.next) {
                current = current.next
            }
            current.next = node
        }
        this.length++
    }
    insert(position, el) {
        if (position < 0 || position >= this.length) {
            return false
        }
        const node = new Node(el)
        if (position === 0) {
            node.next = this.head
            this.head = node
        } else {
            let current = this.head
            while (--position) {
                current = current.next
            }
            node.next = current.next
            current.next = node
        }
        this.length++
        return true
    }
    indexOf(el) {
        let current = this.head
        let index = 0
        while (current) {
            if (el === current.element) return index
            index++
            current = current.next
        }
        return -1
    }
    remove(el) {
        return this.removeAt(this.indexOf(el))
    }
    removeAt(position) {
        if (position < 0 || position >= this.length) return false

        let current = this.head
        if (position === 0) {
            this.head = current.next
        } else {
            let index = 0
            let previous
            while (index++ < position) {
                previous = current
                current = current.next
            }
            previous.next = current.next
        }
        current.next = null
        this.length--
        return true
    }
    getHead() {
        return this.head
    }
    size() {
        return this.length
    }
    isEmpty() {
        return this.length === 0
    }
    toString() {
        let res = ''
        let current = this.head
        while (current) {
            res += current.element
            current = current.next
        }
        return res
    }
}

JavaScript 与简单算法

铃予 2022-05-03 00:14:29

补充一下:
extends 如果单纯用在ts类型系统中, 起到if作用, 例如:

type DropChar<
  S extends string,
  C extends string
> = S extends `${infer F}${infer R}`
  ? F extends C
    ? DropChar<R, C>
    : `${F}${DropChar<R, C>}`
  : S;

implements 后面 接的是 interface 用于 约束某一个类必须用哪一些属性, 方法

第 222 题:implements 与 extends 的区别

更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文