猫史档案馆


仅用24行就能完成一个折叠背包的代码?Map满足你(什

用户:yee剑走天下yee剑走天下查看:12 回复:10 评论:12 创建时间:2023-07-25T12:01:15


class Bag {
    constructor(initialValue = []) {
        this.item = new Map(initialValue);
    }
    addThings(name, num) {
        this.item.has(name) ? this.item.set(name, this.item.get(name) + num) : this.item.set(name, num)
    }
    delThings(name, num) {
        this.item.has(name) ? this.item.set(name, this.item.get(name) - num) :null;
        this.item.get(name) <= 0 ? this.item.delete(name) : null
    }
    hasThings(name, num = 1) {
        return (this.item.get(name) || 0) >= num;
    }
    length() {
        return this.item.size
    }
    clear() {
        this.item.clear()
    }
    showBag() {
        return Array.from(this.item, ([name, num]) => `${name} × ${num}件`);
    }
}

又氵了一个帖(确信


回复

上一页1 页 / 共 1下一页
Neural_NetworkNeural_Network

dddd()

点赞0


评论


SCS_user_LoeheodVOQSCS_user_LoeheodVOQ

Map相当于python的dict?

点赞0


评论


云游box3der尖头叉子云游box3der尖头叉子

qpzc()

点赞0


评论


暗夜极光暗夜极光

收藏)emotion_编程猫_厉害了emotion_编程猫_搓头emotion_编程猫_加油emotion_编程猫_溜了溜了

点赞0


评论


指令者指令者

ddddddddddddddddddddddd

点赞0


评论


屑三芙化氮awa~屑三芙化氮awa~

我顶顶顶

点赞0


评论


yee剑走天下yee剑走天下

ddd

点赞0


评论


神奇代码岛func王清林神奇代码岛func王清林

点赞0


评论


懒人一个a懒人一个a

(哇喔)

点赞0


评论


往事随风4434往事随风4434

有没有图形化的

点赞0


评论