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
	}
}

本文完,感谢阅读

相关推荐
浩浩测试一下6 分钟前
渗透信息收集- Web应用漏洞与指纹信息收集以及情报收集
android·前端·安全·web安全·网络安全·安全架构
Orlando cron24 分钟前
数据结构入门:链表
数据结构·算法·链表
西陵32 分钟前
Nx带来极致的前端开发体验——借助CDD&TDD开发提效
前端·javascript·架构
小磊哥er43 分钟前
【前端工程化】前端工作中的业务规范有哪些
前端
ᥬ 小月亮1 小时前
webpack基础
前端·webpack
牛客企业服务1 小时前
2025年AI面试推荐榜单,数字化招聘转型优选
人工智能·python·算法·面试·职场和发展·金融·求职招聘
YongGit1 小时前
探索 AI + MCP 渲染前端 UI
前端·后端·node.js
慧一居士2 小时前
<script setup>中的setup作用以及和不带的区别对比
前端
糖葫芦君2 小时前
Policy Gradient【强化学习的数学原理】
算法
RainbowSea2 小时前
NVM 切换 Node 版本工具的超详细安装说明
java·前端