黄山秋叶 發表於 2021-8-2 10:15:00

用 JavaScript 刷 LeetCode 的正确姿势【进阶】

<p>之前写了篇文章 用JavaScript刷LeetCode的正确姿势,简单总结一些用 JavaScript 刷力扣的基本调试技巧。最近又刷了点题,总结了些数据结构和算法,希望能对各为 JSer 刷题提供帮助。</p>
<p>此篇文章主要想给大家一些开箱即用的 JavaScipt 版本的代码模板,涉及到较复杂的知识点,原理部分可能会省略,有需要的话后面有时间可以给部分知识点单独写一篇详细的讲解。</p>
<p><small>走过路过发现 bug 请指出,拯救一个辣鸡(但很帅)的少年就靠您啦!!!</small></p>
<h2 id="bigint">BigInt</h2>
<p>众所周知,JavaScript 只能精确表达 <code>Number.MIN_SAFE_INTEGER(-2^53+1)</code> ~ <code>Number.MAX_SAFE_INTEGER(2^53-1)</code> 的值。</p>
<p>而在一些题目中,常常会有较大的数字计算,这时就会产生误差。举个栗子:在控制台输入下面的两个表达式会得到相同的结果:</p>
<pre><code class="language-js">&gt;&gt; 123456789*123456789      // 15241578750190520
&gt;&gt; 123456789*123456789+1    // 15241578750190520
</code></pre>
<p>而如果使用 BigInt 则可以精确求值:</p>
<pre><code class="language-js">&gt;&gt; BigInt(123456789)*BigInt(123456789)            // 15241578750190521n
&gt;&gt; BigInt(123456789)*BigInt(123456789)+BigInt(1)    // 15241578750190522n
</code></pre>
<p>可以通过在一个整数字面量后面加 <code>n</code> 的方式定义一个 <code>BigInt</code> ,如:<code>10n</code>,或者调用函数 <code>BigInt()</code>。上面的表达式也可以写成:</p>
<pre><code class="language-js">&gt;&gt; 123456789n*123456789n       // 15241578750190521n
&gt;&gt; 123456789n*123456789n+1n    // 15241578750190522n
</code></pre>
<p><code>BigInt</code> 只能与 <code>BigInt</code> 做运算,如果和 <code>Number</code> 进行计算需要先通过 <code>BigInt()</code> 做类型转换。</p>
<p><code>BigInt</code> 支持运算符,<code>+</code>、<code>*</code>、<code>-</code>、<code>**</code>、<code>%</code> 。除 <code>&gt;&gt;&gt;</code>(无符号右移)之外的<strong>位操作</strong>也可以支持。因为 <code>BigInt</code> 都是有符号的, <code>&gt;&gt;&gt;</code>(无符号右移)不能用于 <code>BigInt</code>。<code>BigInt</code> 不支持单目 (<code>+</code>) 运算符。</p>
<p><code>BigInt</code> 也支持 <code>/</code> 运算符,但是会被向上取整。</p>
<pre><code class="language-js">const rounded = 5n / 2n; // 2n, not 2.5n
</code></pre>
<h2 id="取模运算">取模运算</h2>
<p>在数据较大时,一般没有办法直接去进行计算,通常都会给一个大质数(例如,<code>1000000007</code>),求对质数取模后的结果。</p>
<p><strong>取模运算的常用性质:</strong></p>
<pre><code class="language-js">(a + b) % p = (a % p + b % p) % p
(a - b) % p = (a % p - b % p) % p
(a * b) % p = (a % p * b % p) % p
a ^ b % p = ((a % p) ^ b) % p
</code></pre>
<p>可以看出,加/减/乘/乘方,都可直接在运算的时候取模,至于除法则会复杂一些,稍后再讲。</p>
<p>举一个例子,LeetCode 1175. 质数排列</p>
<blockquote>
<p>请你帮忙给从 <code>1</code> 到 <code>n</code>&nbsp;的数设计排列方案,使得所有的「质数」都应该被放在「质数索引」(索引从 <code>1</code> 开始)上;你需要返回可能的方案总数。</p>
<p>让我们一起来回顾一下「质数」:质数一定是大于 <code>1</code> 的,并且不能用两个小于它的正整数的乘积来表示。</p>
<p>由于答案可能会很大,所以请你返回答案 <strong>模 mod</strong>&nbsp;<code>10^9 + 7</code>&nbsp;之后的结果即可。</p>
</blockquote>
<p>题目很简单,先求出质数的个数 <code>x</code>,则答案为 <code>x!(n-x)!</code>(不理解的可以去看题解区找题解,这里就不详细解释了)</p>
<p>由于阶乘的值很大,所以在求阶乘的时候需要在运算时取模,同时这里用到了上面所说的<code>BigInt</code>。</p>
<pre><code class="language-js">/**
* @param {number} n
* @return {number}
*/
var numPrimeArrangements = function(n) {
    const mod = 1000000007n;
    // 先把100以内的质数打表(不想再写判断质数的代码了
    const prime = ;
    // 预处理阶乘
    const fac = new Array(n + 1);
    fac = 1n; // 要用bigint
    for (let i = 1; i &lt;= n; i++) {
      fac = fac * BigInt(i) % mod;
    }
    // 先求n以内的质数的个数
    const x = prime.filter(i =&gt; i &lt;= n).length;
    // x!(n-x)!
    return fac * fac % mod;
};
</code></pre>
<h2 id="快速幂">快速幂</h2>
<p>快速幂,顾名思义,快速求幂运算。原理也很简单,比如我们求 <code>x^10</code> 我们可以求 <code>(x^5)^2</code> 可以减少一半的运算。</p>
<p>假设我们求 <code>(x^n)</code></p>
<ul>
<li>如果 <code>n</code> 是偶数,变为求 <code>(x^(n/2))^2</code></li>
<li>如果 <code>n</code> 是奇数,则求 <code>(x^⌊n/2⌋)^2 * x</code> (<code>⌊⌋</code> 是向下取整)</li>
</ul>
<p>因为快速幂涉及到的题目一般数据都很大,需要取模,所以加了取模运算。其中,代码中 <code>n&gt;&gt;=1</code> 相当于 <code>n=n/2</code>,<code>if(n&amp;1)</code>是在判断<code>n</code>是否为奇数。</p>
<p>代码如下:</p>
<pre><code class="language-js">// x ^ n % mod
function pow(x, n, mod) {
    let ans = 1;
    while (n &gt; 0) {
      if (n &amp; 1) ans = ans * x % mod;
      x = x * x % mod;
      n &gt;&gt;= 1;
    }
    return ans;
}
</code></pre>
<h2 id="乘法逆元数论倒数">乘法逆元(数论倒数)</h2>
<p>上面说了除法的取模会复杂一些,其实就是涉及了<strong>乘法逆元</strong>。</p>
<p>当我们求 <code>(a/b)%p</code> 你以为会是简单的 <code>((a%p)/(b%p))%p</code>?当然不是!(反例自己想去Orz</p>
<p>假设有 <code>(a*x)%p=1</code> 则称 <code>a</code>和<code>x</code>关于<code>p</code>互为逆元(<code>a</code> 是 <code>x</code> 关于 <code>p</code> 的逆元,<code>x</code> 是 <code>a</code> 关于 <code>p</code> 的逆元)。比如:<code>2*3%5=1</code> 则 <code>2</code> 和 <code>3</code> 关于 <code>5</code> 互为逆元。</p>
<p>我们把 <code>a</code> 的逆元用 <code>inv(a)</code> 表示。那么:</p>
<pre><code class="language-js">(a/b) % p
= ( (a/b) * (b*inv(b)) ) % p // 因为(b*inv(b))为1
= (a * inv(b)) % p
= (a%p * inv(b)%p) % p
</code></pre>
<p>现在通过逆元神奇的把除法运算变没了~~~</p>
<p>问题在于怎么求乘法逆元。有两种方式,<strong>费马小定理</strong> 和 <strong>扩展欧几里德算法</strong></p>
<p>不求甚解的我只记了一种解法,即费马小定理:<code>a^(p-1) ≡ 1 (mod p)</code></p>
<p>由费马小定理我们可以推论:<code>a^(p-2) ≡ inv(a)(mod p)</code></p>
<p>数学家的事我们程序员就不要想那么多啦,记结论就好了。即:</p>
<blockquote>
<p><strong><code>a</code>关于<code>p</code>的逆元为<code>a^(p-2)</code></strong></p>
</blockquote>
<p>好了,现在可以通过快速幂求出 <code>a</code> 的逆元了。</p>
<pre><code class="language-js">function inv(a, p) {
    return pow(a, p - 2, p); // pow是上面定义的快速幂函数
}
</code></pre>
<p>(P.S.其实我数论很烂= =,平时都是直接记结论,所以此处讲解可能存在不准确的情况。仅供参考。</p>
<h2 id="二分答案">二分答案</h2>
<p>解题的时候往往会考虑枚举答案然后检验枚举的值是否正确。若满足单调性,则满足使用二分法的条件。把这里的枚举换成二分,就变成了“二分答案”。二分答案的时间复杂度是<code>O(logN * (单次验证当前值是否满足条件的复杂度))</code></p>
<p>很多同学在边界问题上经常出bug,也会不小心写个死循环什么的,我总结了一个简单清晰不会出错的二分模板:</p>
<pre><code class="language-js">// isValid 判断某个值是否合法 根据题目要求实现
// 假设 如果x合法则大于x一定合法 如果x不合法则小于x一定不合法
// 求最小合法值
function binaryCalc() {
    let l = 0, r = 10000;   // 答案可能出现的最小值l和最大值r 根据题目设置具体值
    let ans;    // 最终答案
    while (l &lt;= r) {
      let mid = (l + r) &gt;&gt; 1; // 位运算取中间值 相当于 floor((l+r)/2)
      if (isValid(mid)) {
            // 如果 mid 合法 则 都是合法的
            // 我们先把ans设置为当前获取的合法值的最小值 mid
            ans = mid;
            // 然后再去继续去求里面是否有合法值
            r = mid - 1;
      } else {
            // 如果mid不合法 则都是不合法的
            // 我们去中找答案
            l = mid + 1;
      }
    }
    return ans;
}
</code></pre>
<p>举一个简单的例子,LeetCode 69. x 的平方根 是一个二分模板题。题目要求是,给一个数字 <code>x</code> 求平方小于等于 <code>x</code>的最大整数。此处求的是最大值,和模板中对<code>l</code>和<code>r</code>的处理刚好相反。</p>
<pre><code class="language-js">/**
* @param {number} x
* @return {number}
*/
var mySqrt = function(x) {
    let l = 0, r = x; // 根据题目要求 答案可能的值最小为0 最大为x
    let ans = 0;      // 最终答案
   
    function isValid(v) {       // 判断一个数是否合法
      return v * v &lt;= x;
    }

    while (l &lt;= r) {
      let mid = (l + r) &gt;&gt; 1; // 取中间值
      if (isValid(mid)) {
            ans = mid;
            l = mid + 1;
      } else {
            r = mid - 1;
      }
    }
    return ans;
};
</code></pre>
<h2 id="并查集">并查集</h2>
<p>个人觉得并查集是非常精妙且简洁优雅的数据结构,推荐学习。</p>
<p>并查集应用场景为,存在一些元素,分别包含在不同集合中,需要快速合并两个集合,同时可快速求出两个元素是否处于同一集合。</p>
<p>简单的理解并查集的实现,就是把每一个集合都当做一棵树,每个节点都有一个父节点,每棵树都有一个根节点(根节点的父节点为其本身)。</p>
<p>判断是否同一集合:我们可以顺着节点的父节点找到该节点所在集合的根节点。当我们确定两个集合拥有同一个根节点,则证明两个节点处于同一个集合。</p>
<p>合并操作:分别取得两个节点所在集合的根节点,把其中一个根节点的父节点设置为另一个根节点即可。</p>
<p>可能说的比较抽象,想详细了解的同学可以自己深入学习,这里直接给出代码模板。</p>
<pre><code class="language-js">class UnionFind {
    constructor(n) {
      this.n = n; // 节点个数
      // 记录每个节点的父节点 初始时每个节点自己为一个集合 即每个节点的父节点都是其本身
      this.father = new Array(n).fill().map((v, index) =&gt; index);
    }
    // 寻找一个节点的根节点
    find(x) {
      // 如果父节点为其本身 则证明是根节点
      if (x == this.father) {
            return x;
      }
      // 递归查询
      // 此处进行了路径压缩 即将x的父节点直接设置为根节点 下一次查询的时候 将减少递归次数
      return this.father = this.find(this.father);
    }
    // 合并x和y所在的两个集合
    merge(x, y) {
      const xRoot = this.find(x); // 找到x的根节点
      const yRoot = this.find(y); // 找到y的根节点
      this.father = yRoot; // 将xRoot的父节点设置为yRoot 即可将两个集合合并
    }
    // 计算集合个数
    count() {
      // 其实就是查询根节点的个数
      let cnt = 0;
      for (let i = 0; i &lt; this.n; i++) {
            if (this.father === i) { // 判断是否为根节点
                cnt++;
            }
      }
      return cnt;
    }
}
</code></pre>
<p>找一个并查集的题目,方便大家理解并查集的妙处。并查集的题目可以出得非常灵活,可能不会轻易看出是并查集。 LeetCode 947. 移除最多的同行或同列石头<br>
</p>
<blockquote>
<p><code>n</code> 块石头放置在二维平面中的一些整数坐标点上。每个坐标点上最多只能有一块石头。</p>
<p>如果一块石头的 <strong>同行或者同列</strong> 上有其他石头存在,那么就可以移除这块石头。</p>
<p>给你一个长度为 <code>n</code> 的数组 <code>stones</code> ,其中 <code>stones = </code> 表示第 <code>i</code> 块石头的位置,返回 <strong>可以移除的石子</strong> 的最大数量。</p>
</blockquote>
<p>此处参考了官方的题解</p>
<p>把二维坐标平面上的石头想象成图的顶点,如果两个石头横坐标相同、或者纵坐标相同,在它们之间形成一条边。</p>
<p><img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3b64e61f96274e5fa20d8166861700d4~tplv-k3u1fbpfcp-watermark.image" alt="image.png" loading="lazy"></p>
<p>根据可以移除石头的规则:如果一块石头的 <strong>同行或者同列</strong> 上有其他石头存在,那么就可以移除这块石头。可以发现:一定可以把一个连通图里的所有顶点根据这个规则删到只剩下一个顶点。</p>
<p>我们遍历所有的石头,发现如果有两个石头的横坐标或者纵坐标相等,则证明这两块石头应该在同一个集合(即上面说的连通图)里。那么最后每个集合只留一块石头,剩下的则全部可以被移除。</p>
<p>AC代码:</p>
<pre><code class="language-js">// 定义 UnionFind 相关代码
/**
* @param {number[][]} stones
* @return {number}
*/
var removeStones = function(stones) {
    let n = stones.length;
    let uf = new UnionFind(n);
    for (let i = 0; i &lt; n; i++) {
      for (let j = i + 1; j &lt; n; j++) {
            // 有两个石头的横坐标或者纵坐标相等 则合并
            if (stones == stones || stones == stones) {
                uf.merge(i, j);
            }
      }
    }
    // 石头总数减去集合的个数就是答案
    return n - uf.count();
};
</code></pre>
<h2 id="kmp">KMP</h2>
<p>KMP 被一些算法初学者认为是高难度数据结构,一般遇到直接放弃那种。所以我想了下几句话应该也解释不清,那就跳过原理直接上模板吧。😛</p>
<p>先简单说一下背景,KMP 解决的是子串查找的问题。给两个字符串<code>S</code>和<code>T</code>,求<code>T</code>是否是<code>S</code>的子串。解决方法是先预处理<code>T</code>,求出<code>T</code>的<code>next</code>数组,其中<code>next</code>代表<code>T</code>的子串<code>T</code>(即<code>T.substring(0, i)</code>)<strong>最长相等的前缀后缀</strong> 的长度。</p>
<p>嘛,最长相等的前缀后缀,就是说,比如字符串<code>"abcuuabc"</code>最长相等的前缀后缀就是<code>abc</code>,那么其长度就应该是<code>3</code>。</p>
<p>然后借助<code>next</code>数组,可以在线性时间复杂度内求出<code>T</code>是否为<code>S</code>的子串,首次出现下标,以及出现次数。</p>
<p>模板代码:</p>
<pre><code class="language-js">// 求字符串 s 的 next 数组
function getNext(s) {
    let len = s.length;
    let next = new Array(len + 1);
    let j = 0, k = -1;
    next = -1;
    while (j &lt; len) {
      if (k == -1 || s === s) next[++j] = ++k;
      else k = next;
    }
    return next;
}
// 求字符串 t 在字符串 s 中第一次出现的下标 不存在则返回 -1
function findIndex(s, t) {
    let i = 0, j = 0;
    let next = getNext(t);
    let slen = s.length, tlen = t.length;
    while (i &lt; slen &amp;&amp; j &lt; tlen) {
      if (j === -1 || s === t) ++i, ++j;
      else j = next;
    }
    return j === tlen ? i - tlen : -1;
}
// 求字符串 t 在字符串 s 出现的次数
function findCount(s, t) {
    let ans = 0;
    let i = 0, j = 0;
    let next = getNext(t);
    let slen = s.length, tlen = t.length;
    while (i &lt; slen &amp;&amp; j &lt; tlen) {
      if (j === -1 || s === t) ++i, ++j;
      else j = next;
      if (j === tlen) {
            ++ans;
            j = next;
      }
    }
    return ans;
}
</code></pre>
<p>如果多次计算子串相同的话,<code>next</code>数组可以预处理,不需要每次在求<code>index</code>时再计算。</p>
<p>举个例子吧,LeetCode 1392. 最长快乐前缀</p>
<blockquote>
<p>「快乐前缀」是在原字符串中既是&nbsp;<strong>非空</strong> 前缀也是后缀(不包括原字符串自身)的字符串。</p>
<p>给你一个字符串 s,请你返回它的 <strong>最长快乐前缀</strong>。</p>
<p>如果不存在满足题意的前缀,则返回一个空字符串。</p>
</blockquote>
<p>我们会发现这不就是 <code>next</code> 数组么,所以我记得这次周赛会 KMP 的同学直接 copy 就得分了.....</p>
<p>AC代码;</p>
<pre><code class="language-js">// getNext 定义参考上面模板
/**
* @param {string} s
* @return {string}
*/
var longestPrefix = function(s) {
    let len = s.length;
    let next = getNext(s);
    let ansLen = next == len ? len - 1 : next; // 不包含原字符串 需要特殊判断下
    return s.substring(0, ansLen);
};
</code></pre>
<p>再来一个 LeetCode 28. 实现 strStr() 求一个字符串在另一个字符串中首次出现的位置,就是<code>indexOf</code>的实现,其实也就是模板中的 <code>findIndex</code> 函数。</p>
<p>AC代码:</p>
<pre><code class="language-js">// findIndex 定义参考模板
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function(haystack, needle) {
    return findIndex(haystack, needle);
};
</code></pre>
<h2 id="优先队列堆">优先队列(堆)</h2>
<p>优先队列,我们给每个元素定义优先级,每次取队列中的值都取的是优先级最大的数。</p>
<p>其他的语言中都自带优先队列的实现,JSer就只能QAQ……所以我自己写了一个优先队列,就是通过堆来实现。(原理就不讲啦,学过堆排序的应该懂~(趴</p>
<pre><code class="language-js">class PriorityQueue {
    /**
   * 构造函数 可以传入比较函数自定义优先级 默认是最小值排在最前
   * @param {function} compareFunc 比较函数 compareFunc(a, b) 为 true 表示 a 的优先级 &gt; b
   */
    constructor(compareFunc) {
      this.queue = [];
      this.func = compareFunc || ((a, b) =&gt; a &lt; b);
    }
    /**
   * 向优先队列添加一个元素
   */
    push(ele) {
      this.queue.push(ele);
      this.pushup(this.size() - 1)
    }
    /**
   * 弹出最小值并返回
   */
    pop() {
      let { queue } = this;
      if (queue.length &lt;= 1) return queue.pop();
      
      let min = queue;
      queue = queue.pop();
      this.pushdown(0);
      return min;
    }
    /**
   * 返回最小值
   */
    top() {
      return this.size() ? this.queue : null;
    }
    /**
   * 返回队列中元素的个数
   */
    size() {
      return this.queue.length;
    }
    /**
   * 初始化堆
   */
    setQueue(queue) {
      this.queue = queue;
      for (let i = (this.size() &gt;&gt; 1); i &gt;= 0; i--) {
            this.pushdown(i);
      }
    }
    /**
   * 调整以保证 queue 是子树中最小的
   * */
    pushdown(index) {
      let { queue, func } = this;
      let fa = index;
      let cd = index * 2 + 1;
      let size = queue.length;
      while (cd &lt; size) {
            if (cd + 1 &lt; size &amp;&amp; func(queue, queue)) cd++;
            if (func(queue, queue)) break;
            // 交换 queue 和 queue
            , queue] = , queue];
            // 继续处理子树
            fa = cd;
            cd = fa * 2 + 1;
      }
    }
    /**
   * 调整 index 到合法位置
   */
    pushup(index) {
      let { queue, func } = this;
      while (index) {
            const fa = (index - 1) &gt;&gt; 1;
            if (func(queue, queue)) {
                break;
            }
            , queue] = , queue];
            index = fa;
      }
    }
}
</code></pre>
<p>举个例子,LeetCode 23. 合并K个升序链表 一道困难题目哦~</p>
<blockquote>
<p>给你一个链表数组,每个链表都已经按升序排列。</p>
<p>请你将所有链表合并到一个升序链表中,返回合并后的链表。</p>
</blockquote>
<p>做法很简单,把链表都放到优先队列里,每次取值最小的链表就行。具体实现看代码。</p>
<pre><code class="language-js">/**
* @param {ListNode[]} lists
* @return {ListNode}
*/
var mergeKLists = function(lists) {
    let queue = new PriorityQueue((a, b) =&gt; a.val &lt; b.val);

    lists.forEach(list =&gt; {
      list &amp;&amp; queue.push(list);
    });

    const dummy = new ListNode(0);
    let cur = dummy;

    while (queue.size()) {
      let node = queue.pop();
      if (node.next) queue.push(node.next);
      cur.next = new ListNode(node.val);
      cur = cur.next;
    }

    return dummy.next;
};
</code></pre>
<h2 id="trie字典树前缀树">Trie(字典树/前缀树)</h2>
<p>字典树应该算是一个比较简单而且直观的数据结构~字典树模板题可以看 LeetCode 208. 实现 Trie (前缀树)</p>
<pre><code class="language-js">/**
* Initialize your data structure here.
*/
var Trie = function() {
    this.nodes = [];
};

/**
* Inserts a word into the trie.
* @param {string} word
* @return {void}
*/
Trie.prototype.insert = function(word) {
    let nodes = this.nodes;
    for (let w of word) {
      if (!nodes) {
            nodes = {};
      }
      nodes = nodes;
    }
    nodes.end = true;
};

/**
* Returns if the word is in the trie.
* @param {string} word
* @return {boolean}
*/
Trie.prototype.search = function(word) {
    let nodes = this.nodes;
    for (let w of word) {
      if (!nodes) {
            return false;
      }
      nodes = nodes;
    }
    return !!nodes.end;
};

/**
* Returns if there is any word in the trie that starts with the given prefix.
* @param {string} prefix
* @return {boolean}
*/
Trie.prototype.startsWith = function(prefix) {
    let nodes = this.nodes;
    for (let w of prefix) {
      if (!nodes) {
            return false;
      }
      nodes = nodes;
    }
    return true;
};
</code></pre>
<p>字典树的变种应用,LeetCode 421. 数组中两个数的最大异或值 参考:题解</p>
<p>我们也可以将数组中的元素看成长度为 <code>31</code> 的字符串,字符串中只包含 <code>0</code> 和 <code>1</code>。如果我们将字符串放入字典树中,那么在字典树中查询一个字符串的过程,恰好就是从高位开始确定每一个二进制位的过程。对于一个数求异或和的最大值,就是从最高位开始,每一位都找异或和最大的那个分支。</p>
<pre><code class="language-js">var Trie = function() {
    this.nodes = [];
};
Trie.prototype.insert = function(digit) {
    let nodes = this.nodes;
    for (let d of digit) {
      if (!nodes) {
            nodes = [];
      }
      nodes = nodes;
    }
};
Trie.prototype.maxXor = function(digit) {
    let xor = 0;
    let nodes = this.nodes;
    for (let i = 0; i &lt; digit.length; i++) {
      let d = digit;
      if (nodes) {
            xor += 1 &lt;&lt; (digit.length - i - 1);
            nodes = nodes;
      } else {
            nodes = nodes;
      }
    }
    return xor;
};

/**
* @param {number[]} nums
* @return {number}
*/
var findMaximumXOR = function(nums) {
    let trie = new Trie();
    let maxXor = 0;
    for (let x of nums) {
      let binaryX = x.toString(2);
      // 因为 0 &lt;= nums &lt;= 2^31 - 1 所以最多为31位
      // 补前缀0统一变成31位
      binaryX = ('0'.repeat(31) + binaryX).substr(-31);
      // 插入Trie
      trie.insert(binaryX);
      maxXor = Math.max(maxXor, trie.maxXor(binaryX));
    }
    return maxXor;
};
</code></pre>
<h2 id="总结">总结</h2>
<p>暂时就想到这么多比较常见的数据结构。如果有其他的可以在评论区补充,如果我会的话会后续加上的。</p>
<p>JSer冲鸭!!!</p>
<h2 id="参考资料">参考资料</h2>
<ul>
<li>https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/BigInt</li>
<li>https://www.cnblogs.com/linyujun/p/5194184.html</li>
</ul><br><br>
来源:https://www.cnblogs.com/wenruo/p/15088807.html
頁: [1]
查看完整版本: 用 JavaScript 刷 LeetCode 的正确姿势【进阶】