用户:
霸道的笨笨鸭oZ1P查看:2 回复:1 评论:2 创建时间:2023-08-13T11:35:48
有人能帮我翻一下yee剑的最新版折叠背包代码吗QwQ,我这边只能翻五页后面的就看不到了
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}件`);
}
}
点赞0
评论