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

本文完,感谢阅读

相关推荐
春日见1 分钟前
自动驾驶数据驱动规控进化之路
运维·服务器·人工智能·深度学习·算法·机器学习·自动驾驶
ai_coder_ai3 分钟前
使用web ide开发和调试自动化脚本
前端·ide·自动化
kaikaile19958 分钟前
盲源分离(BSS)程序代码:信号处理与模态识别
前端·信号处理
Byron__9 分钟前
数据库高频面试核心知识点
数据库·面试
普贤莲花9 分钟前
【【2026年第22周---写于20260531】---好好工作,好好生活】
程序人生·算法·leetcode·生活
Bingorl10 分钟前
机器学习之聚类算法
算法·机器学习·聚类
এ慕ོ冬℘゜12 分钟前
从零封装企业级通用确认弹窗组件|高复用、低耦合、适配全场景
开发语言·前端·javascript
Bigger12 分钟前
现在面试官竟然这么问问题,你知道吗?😮
前端·人工智能·agent
小欣加油14 分钟前
leetcode2126 摧毁小行星
数据结构·c++·算法·leetcode·职场和发展
菜菜的顾清寒14 分钟前
力扣HOT100(45) 二叉树的直径
算法·leetcode·职场和发展