30天刷题挑战(二十五)

题目来源: LeetCode 75 30 天 JavaScript 挑战

208. 实现 Trie (前缀树)

代码

js 复制代码
var Trie = function() {
  this.node = new Map()
};

/** 
 * @param {string} word
 * @return {void}
 */
Trie.prototype.insert = function(word) {
    let node = this.node

    for (let ch of word) {
       if (!node.has(ch)) {
         node.set(ch, new Map())
       }
       node = node.get(ch)
    }
    node.set("isEnd", true)
};


Trie.prototype.searchPrefix = function(prefix) {
  let node = this.node

   for (let ch of prefix) {
       if (!node.has(ch)) {
         return false
       }
       node = node.get(ch)
    }

  return node
};


/** 
 * @param {string} word
 * @return {boolean}
 */
Trie.prototype.search = function(word) {
  let node = this.searchPrefix(word)
  return !!node && !!node.get("isEnd")
};

/** 
 * @param {string} prefix
 * @return {boolean}
 */
Trie.prototype.startsWith = function(prefix) {
    return this.searchPrefix(prefix)
};

/**
 * Your Trie object will be instantiated and called as such:
 * var obj = new Trie()
 * obj.insert(word)
 * var param_2 = obj.search(word)
 * var param_3 = obj.startsWith(prefix)
 */

1268. 搜索推荐系统

思路

模拟法,按题意匹配字符

代码

js 复制代码
/**
 * @param {string[]} products
 * @param {string} searchWord
 * @return {string[][]}
 */
var suggestedProducts = function(products, searchWord) {
  let res = []

  products.sort()

  for (let i = 0; i < searchWord.length; i++) {
    let temp = []

    products.forEach(p => {
      if (p[i] === searchWord[i]) {
        temp.push(p)
      }
    })

    products = temp
    res.push(products.length > 3 ? products.slice(0, 3) :products) 
  }

 return res
};

2726. 使用方法链的计算器

代码

js 复制代码
class Calculator {
  
    /** 
     * @param {number} value
     */
	constructor(value) {
		this.result = value
	}

    /** 
     * @param {number} value
     * @return {Calculator}
     */
	add(value){
		this.result += value
    return this
	}

    /** 
     * @param {number} value
     * @return {Calculator}
     */
	subtract(value){
		this.result -= value
    return this
	}

    /** 
     * @param {number} value
     * @return {Calculator}
     */  
	multiply(value) {
		this.result *= value
    return this
	}

    /** 
     * @param {number} value
     * @return {Calculator}
     */
	divide(value) {
    if (!value) {
      throw Error("Division by zero is not allowed")
    }
		this.result /= value
    return this
	}
  
    /** 
     * @param {number} value
     * @return {Calculator}
     */
	power(value) {
		this.result **= value
    return this
	}
    
    /** 
     * @return {number}
     */
	getResult() {
		return this.result
	}
}

本文完,感谢阅读

相关推荐
阿泽·黑核几秒前
Easy Vibe Coding 学习心得(三):前端之美——从设计稿到精美界面
前端·vibe coding·easy vibe
2501_92495269几秒前
C++中的适配器模式
开发语言·c++·算法
智驱力人工智能6 分钟前
馆藏文物预防性保护依赖的图像分析技术 文物损害检测 文物破损检测 文物损害识别误报率优化方案 文物安全巡查AI系统案例 智慧文保AI监测
人工智能·算法·安全·yolo·边缘计算
wuguan_7 分钟前
Halcon中值滤波,均值滤波,高斯滤波
算法·halcon
☆5667 分钟前
C++安全编程指南
开发语言·c++·算法
tobias.b8 分钟前
机器学习 超清晰通俗讲解 + 核心算法全解(深度+易懂版)
人工智能·算法·机器学习
无心水9 分钟前
【时间利器】4、JavaScript时间处理全解:Date/moment/dayjs/Temporal
开发语言·前端·javascript·状态模式·openclaw·date/moment·dayjs/temporal
踩着两条虫15 分钟前
AI 驱动的 Vue3 应用开发平台 深入探究(二十五):API与参考之Renderer API 参考
前端·vue.js·人工智能
踩着两条虫15 分钟前
AI 驱动的 Vue3 应用开发平台 深入探究(二十四):API与参考之Provider API 参考
前端·vue.js·ai编程
budingxiaomoli17 分钟前
优选算法--bfs解决FloodFill问题
算法·宽度优先