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

本文完,感谢阅读

相关推荐
yzx99101312 分钟前
基于 Q-Learning 算法和 CNN 的强化学习实现方案
人工智能·算法·cnn
亮亮爱刷题15 分钟前
算法练习-回溯
算法
眼镜哥(with glasses)1 小时前
蓝桥杯 国赛2024python(b组)题目(1-3)
数据结构·算法·蓝桥杯
coding随想5 小时前
JavaScript ES6 解构:优雅提取数据的艺术
前端·javascript·es6
小小小小宇6 小时前
一个小小的柯里化函数
前端
灵感__idea6 小时前
JavaScript高级程序设计(第5版):无处不在的集合
前端·javascript·程序员
小小小小宇6 小时前
前端双Token机制无感刷新
前端
小小小小宇6 小时前
重提React闭包陷阱
前端
int型码农6 小时前
数据结构第八章(一) 插入排序
c语言·数据结构·算法·排序算法·希尔排序
小小小小宇6 小时前
前端XSS和CSRF以及CSP
前端