mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
update
This commit is contained in:
parent
2a71c78585
commit
f3b23d1fc3
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2022.05.02**
|
||||
> 最后更新日期: **2022.05.13**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
158
leetcode-cn/originData/count-number-of-texts.json
Normal file
158
leetcode-cn/originData/count-number-of-texts.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,51 @@
|
||||
<p>给你一个字符串 <code>num</code> ,表示一个大整数。如果一个整数满足下述所有条件,则认为该整数是一个 <strong>优质整数</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li>该整数是 <code>num</code> 的一个长度为 <code>3</code> 的 <strong>子字符串</strong> 。</li>
|
||||
<li>该整数由唯一一个数字重复 <code>3</code> 次组成。</li>
|
||||
</ul>
|
||||
|
||||
<p>以字符串形式返回 <strong>最大的优质整数</strong> 。如果不存在满足要求的整数,则返回一个空字符串 <code>""</code> 。</p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><strong>子字符串</strong> 是字符串中的一个连续字符序列。</li>
|
||||
<li><code>num</code> 或优质整数中可能存在 <strong>前导零</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = "6<em><strong>777</strong></em>133339"
|
||||
<strong>输出:</strong>"777"
|
||||
<strong>解释:</strong>num 中存在两个优质整数:"777" 和 "333" 。
|
||||
"777" 是最大的那个,所以返回 "777" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = "23<em><strong>000</strong></em>19"
|
||||
<strong>输出:</strong>"000"
|
||||
<strong>解释:</strong>"000" 是唯一一个优质整数。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = "42352338"
|
||||
<strong>输出:</strong>""
|
||||
<strong>解释:</strong>不存在长度为 3 且仅由一个唯一数字组成的整数。因此,不存在优质整数。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= num.length <= 1000</code></li>
|
||||
<li><code>num</code> 仅由数字(<code>0</code> - <code>9</code>)组成</li>
|
||||
</ul>
|
@ -0,0 +1,54 @@
|
||||
<p>一个括号字符串是一个 <strong>非空</strong> 且只包含 <code>'('</code> 和 <code>')'</code> 的字符串。如果下面 <strong>任意</strong> 条件为 <strong>真</strong> ,那么这个括号字符串就是 <strong>合法的</strong> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>字符串是 <code>()</code> 。</li>
|
||||
<li>字符串可以表示为 <code>AB</code>(<code>A</code> 连接 <code>B</code>),<code>A</code> 和 <code>B</code> 都是合法括号序列。</li>
|
||||
<li>字符串可以表示为 <code>(A)</code> ,其中 <code>A</code> 是合法括号序列。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个 <code>m x n</code> 的括号网格图矩阵 <code>grid</code> 。网格图中一个 <strong>合法括号路径</strong> 是满足以下所有条件的一条路径:</p>
|
||||
|
||||
<ul>
|
||||
<li>路径开始于左上角格子 <code>(0, 0)</code> 。</li>
|
||||
<li>路径结束于右下角格子 <code>(m - 1, n - 1)</code> 。</li>
|
||||
<li>路径每次只会向 <strong>下</strong> 或者向 <strong>右</strong> 移动。</li>
|
||||
<li>路径经过的格子组成的括号字符串是<strong> 合法</strong> 的。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果网格图中存在一条 <strong>合法括号路径</strong> ,请返回 <code>true</code> ,否则返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/example1drawio.png" style="width: 521px; height: 300px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>grid = [["(","(","("],[")","(",")"],["(","(",")"],["(","(",")"]]
|
||||
<b>输出:</b>true
|
||||
<b>解释:</b>上图展示了两条路径,它们都是合法括号字符串路径。
|
||||
第一条路径得到的合法字符串是 "()(())" 。
|
||||
第二条路径得到的合法字符串是 "((()))" 。
|
||||
注意可能有其他的合法括号字符串路径。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/example2drawio.png" style="width: 165px; height: 165px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>grid = [[")",")"],["(","("]]
|
||||
<b>输出:</b>false
|
||||
<b>解释:</b>两条可行路径分别得到 "))(" 和 ")((" 。由于它们都不是合法括号字符串,我们返回 false 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 100</code></li>
|
||||
<li><code>grid[i][j]</code> 要么是 <code>'('</code> ,要么是 <code>')'</code> 。</li>
|
||||
</ul>
|
@ -0,0 +1,38 @@
|
||||
<p>给你一棵二叉树的根节点 <code>root</code> ,找出并返回满足要求的节点数,要求节点的值等于其 <strong>子树</strong> 中值的 <strong>平均值</strong> 。</p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n</code> 个元素的平均值可以由 <code>n</code> 个元素 <strong>求和</strong> 然后再除以 <code>n</code> ,并 <strong>向下舍入</strong> 到最近的整数。</li>
|
||||
<li><code>root</code> 的 <strong>子树</strong> 由 <code>root</code> 和它的所有后代组成。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;">
|
||||
<pre><strong>输入:</strong>root = [4,8,5,0,1,null,6]
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>
|
||||
对值为 4 的节点:子树的平均值 (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4 。
|
||||
对值为 5 的节点:子树的平均值 (5 + 6) / 2 = 11 / 2 = 5 。
|
||||
对值为 0 的节点:子树的平均值 0 / 1 = 0 。
|
||||
对值为 1 的节点:子树的平均值 1 / 1 = 1 。
|
||||
对值为 6 的节点:子树的平均值 6 / 1 = 6 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;">
|
||||
<pre><strong>输入:</strong>root = [1]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>对值为 1 的节点:子树的平均值 1 / 1 = 1。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中节点数目在范围 <code>[1, 1000]</code> 内</li>
|
||||
<li><code>0 <= Node.val <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,50 @@
|
||||
<p>Alice 在给 Bob 用手机打字。数字到字母的 <strong>对应</strong> 如下图所示。</p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 200px; height: 162px;"></p>
|
||||
|
||||
<p>为了 <strong>打出</strong> 一个字母,Alice 需要 <strong>按</strong> 对应字母 <code>i</code> 次,<code>i</code> 是该字母在这个按键上所处的位置。</p>
|
||||
|
||||
<ul>
|
||||
<li>比方说,为了按出字母 <code>'s'</code> ,Alice 需要按 <code>'7'</code> 四次。类似的, Alice 需要按 <code>'5'</code> 两次得到字母 <code>'k'</code> 。</li>
|
||||
<li>注意,数字 <code>'0'</code> 和 <code>'1'</code> 不映射到任何字母,所以 Alice <strong>不</strong> 使用它们。</li>
|
||||
</ul>
|
||||
|
||||
<p>但是,由于传输的错误,Bob 没有收到 Alice 打字的字母信息,反而收到了 <strong>按键的字符串信息</strong> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>比方说,Alice 发出的信息为 <code>"bob"</code> ,Bob 将收到字符串 <code>"2266622"</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个字符串 <code>pressedKeys</code> ,表示 Bob 收到的字符串,请你返回 Alice <strong>总共可能发出多少种文字信息</strong> 。</p>
|
||||
|
||||
<p>由于答案可能很大,将它对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 后返回。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>pressedKeys = "22233"
|
||||
<b>输出:</b>8
|
||||
<strong>解释:</strong>
|
||||
Alice 可能发出的文字信息包括:
|
||||
"aaadd", "abdd", "badd", "cdd", "aaae", "abe", "bae" 和 "ce" 。
|
||||
由于总共有 8 种可能的信息,所以我们返回 8 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>pressedKeys = "222222222222222222222222222222222222"
|
||||
<b>输出:</b>82876089
|
||||
<strong>解释:</strong>
|
||||
总共有 2082876103 种 Alice 可能发出的文字信息。
|
||||
由于我们需要将答案对 10<sup>9</sup> + 7 取余,所以我们返回 2082876103 % (10<sup>9</sup> + 7) = 82876089 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= pressedKeys.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>pressedKeys</code> 只包含数字 <code>'2'</code> 到 <code>'9'</code> 。</li>
|
||||
</ul>
|
@ -0,0 +1,49 @@
|
||||
<p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li>
|
||||
<li>It consists of only one unique digit.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum good </strong>integer as a <strong>string</strong> or an empty string </em><code>""</code><em> if no such integer exists</em>.</p>
|
||||
|
||||
<p>Note:</p>
|
||||
|
||||
<ul>
|
||||
<li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li>
|
||||
<li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "6<strong><u>777</u></strong>133339"
|
||||
<strong>Output:</strong> "777"
|
||||
<strong>Explanation:</strong> There are two distinct good integers: "777" and "333".
|
||||
"777" is the largest, so we return "777".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "23<strong><u>000</u></strong>19"
|
||||
<strong>Output:</strong> "000"
|
||||
<strong>Explanation:</strong> "000" is the only good integer.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "42352338"
|
||||
<strong>Output:</strong> ""
|
||||
<strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= num.length <= 1000</code></li>
|
||||
<li><code>num</code> only consists of digits.</li>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p>A parentheses string is a <strong>non-empty</strong> string consisting only of <code>'('</code> and <code>')'</code>. It is <strong>valid</strong> if <strong>any</strong> of the following conditions is <strong>true</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>It is <code>()</code>.</li>
|
||||
<li>It can be written as <code>AB</code> (<code>A</code> concatenated with <code>B</code>), where <code>A</code> and <code>B</code> are valid parentheses strings.</li>
|
||||
<li>It can be written as <code>(A)</code>, where <code>A</code> is a valid parentheses string.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are given an <code>m x n</code> matrix of parentheses <code>grid</code>. A <strong>valid parentheses string path</strong> in the grid is a path satisfying <strong>all</strong> of the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>The path starts from the upper left cell <code>(0, 0)</code>.</li>
|
||||
<li>The path ends at the bottom-right cell <code>(m - 1, n - 1)</code>.</li>
|
||||
<li>The path only ever moves <strong>down</strong> or <strong>right</strong>.</li>
|
||||
<li>The resulting parentheses string formed by the path is <strong>valid</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <code>true</code> <em>if there exists a <strong>valid parentheses string path</strong> in the grid.</em> Otherwise, return <code>false</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/example1drawio.png" style="width: 521px; height: 300px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [["(","(","("],[")","(",")"],["(","(",")"],["(","(",")"]]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The above diagram shows two possible paths that form valid parentheses strings.
|
||||
The first path shown results in the valid parentheses string "()(())".
|
||||
The second path shown results in the valid parentheses string "((()))".
|
||||
Note that there may be other valid parentheses string paths.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/example2drawio.png" style="width: 165px; height: 165px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[")",")"],["(","("]]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The two possible paths form the parentheses strings "))(" and ")((". Since neither of them are valid parentheses strings, we return false.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 100</code></li>
|
||||
<li><code>grid[i][j]</code> is either <code>'('</code> or <code>')'</code>.</li>
|
||||
</ul>
|
@ -0,0 +1,38 @@
|
||||
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li>
|
||||
<li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [4,8,5,0,1,null,6]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong>
|
||||
For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4.
|
||||
For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5.
|
||||
For the node with value 0: The average of its subtree is 0 / 1 = 0.
|
||||
For the node with value 1: The average of its subtree is 1 / 1 = 1.
|
||||
For the node with value 6: The average of its subtree is 6 / 1 = 6.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
|
||||
<li><code>0 <= Node.val <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p>Alice is texting Bob using her phone. The <strong>mapping</strong> of digits to letters is shown in the figure below.</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 200px; height: 162px;" />
|
||||
<p>In order to <strong>add</strong> a letter, Alice has to <strong>press</strong> the key of the corresponding digit <code>i</code> times, where <code>i</code> is the position of the letter in the key.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, to add the letter <code>'s'</code>, Alice has to press <code>'7'</code> four times. Similarly, to add the letter <code>'k'</code>, Alice has to press <code>'5'</code> twice.</li>
|
||||
<li>Note that the digits <code>'0'</code> and <code>'1'</code> do not map to any letters, so Alice <strong>does not</strong> use them.</li>
|
||||
</ul>
|
||||
|
||||
<p>However, due to an error in transmission, Bob did not receive Alice's text message but received a <strong>string of pressed keys</strong> instead.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, when Alice sent the message <code>"bob"</code>, Bob received the string <code>"2266622"</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given a string <code>pressedKeys</code> representing the string received by Bob, return <em>the <strong>total number of possible text messages</strong> Alice could have sent</em>.</p>
|
||||
|
||||
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> pressedKeys = "22233"
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong>
|
||||
The possible text messages Alice could have sent are:
|
||||
"aaadd", "abdd", "badd", "cdd", "aaae", "abe", "bae", and "ce".
|
||||
Since there are 8 possible messages, we return 8.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> pressedKeys = "222222222222222222222222222222222222"
|
||||
<strong>Output:</strong> 82876089
|
||||
<strong>Explanation:</strong>
|
||||
There are 2082876103 possible text messages Alice could have sent.
|
||||
Since we need to return the answer modulo 10<sup>9</sup> + 7, we return 2082876103 % (10<sup>9</sup> + 7) = 82876089.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= pressedKeys.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>pressedKeys</code> only consists of digits from <code>'2'</code> - <code>'9'</code>.</li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
155
leetcode/originData/count-nodes-equal-to-average-of-subtree.json
Normal file
155
leetcode/originData/count-nodes-equal-to-average-of-subtree.json
Normal file
File diff suppressed because one or more lines are too long
155
leetcode/originData/count-number-of-texts.json
Normal file
155
leetcode/originData/count-number-of-texts.json
Normal file
File diff suppressed because one or more lines are too long
155
leetcode/originData/largest-3-same-digit-number-in-string.json
Normal file
155
leetcode/originData/largest-3-same-digit-number-in-string.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,48 @@
|
||||
<p>A parentheses string is a <strong>non-empty</strong> string consisting only of <code>'('</code> and <code>')'</code>. It is <strong>valid</strong> if <strong>any</strong> of the following conditions is <strong>true</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>It is <code>()</code>.</li>
|
||||
<li>It can be written as <code>AB</code> (<code>A</code> concatenated with <code>B</code>), where <code>A</code> and <code>B</code> are valid parentheses strings.</li>
|
||||
<li>It can be written as <code>(A)</code>, where <code>A</code> is a valid parentheses string.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are given an <code>m x n</code> matrix of parentheses <code>grid</code>. A <strong>valid parentheses string path</strong> in the grid is a path satisfying <strong>all</strong> of the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>The path starts from the upper left cell <code>(0, 0)</code>.</li>
|
||||
<li>The path ends at the bottom-right cell <code>(m - 1, n - 1)</code>.</li>
|
||||
<li>The path only ever moves <strong>down</strong> or <strong>right</strong>.</li>
|
||||
<li>The resulting parentheses string formed by the path is <strong>valid</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <code>true</code> <em>if there exists a <strong>valid parentheses string path</strong> in the grid.</em> Otherwise, return <code>false</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/example1drawio.png" style="width: 521px; height: 300px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [["(","(","("],[")","(",")"],["(","(",")"],["(","(",")"]]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The above diagram shows two possible paths that form valid parentheses strings.
|
||||
The first path shown results in the valid parentheses string "()(())".
|
||||
The second path shown results in the valid parentheses string "((()))".
|
||||
Note that there may be other valid parentheses string paths.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/example2drawio.png" style="width: 165px; height: 165px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[")",")"],["(","("]]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The two possible paths form the parentheses strings "))(" and ")((". Since neither of them are valid parentheses strings, we return false.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 100</code></li>
|
||||
<li><code>grid[i][j]</code> is either <code>'('</code> or <code>')'</code>.</li>
|
||||
</ul>
|
@ -0,0 +1,38 @@
|
||||
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li>
|
||||
<li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [4,8,5,0,1,null,6]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong>
|
||||
For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4.
|
||||
For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5.
|
||||
For the node with value 0: The average of its subtree is 0 / 1 = 0.
|
||||
For the node with value 1: The average of its subtree is 1 / 1 = 1.
|
||||
For the node with value 6: The average of its subtree is 6 / 1 = 6.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
|
||||
<li><code>0 <= Node.val <= 1000</code></li>
|
||||
</ul>
|
48
leetcode/problem/count-number-of-texts.html
Normal file
48
leetcode/problem/count-number-of-texts.html
Normal file
@ -0,0 +1,48 @@
|
||||
<p>Alice is texting Bob using her phone. The <strong>mapping</strong> of digits to letters is shown in the figure below.</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 200px; height: 162px;" />
|
||||
<p>In order to <strong>add</strong> a letter, Alice has to <strong>press</strong> the key of the corresponding digit <code>i</code> times, where <code>i</code> is the position of the letter in the key.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, to add the letter <code>'s'</code>, Alice has to press <code>'7'</code> four times. Similarly, to add the letter <code>'k'</code>, Alice has to press <code>'5'</code> twice.</li>
|
||||
<li>Note that the digits <code>'0'</code> and <code>'1'</code> do not map to any letters, so Alice <strong>does not</strong> use them.</li>
|
||||
</ul>
|
||||
|
||||
<p>However, due to an error in transmission, Bob did not receive Alice's text message but received a <strong>string of pressed keys</strong> instead.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, when Alice sent the message <code>"bob"</code>, Bob received the string <code>"2266622"</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given a string <code>pressedKeys</code> representing the string received by Bob, return <em>the <strong>total number of possible text messages</strong> Alice could have sent</em>.</p>
|
||||
|
||||
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> pressedKeys = "22233"
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong>
|
||||
The possible text messages Alice could have sent are:
|
||||
"aaadd", "abdd", "badd", "cdd", "aaae", "abe", "bae", and "ce".
|
||||
Since there are 8 possible messages, we return 8.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> pressedKeys = "222222222222222222222222222222222222"
|
||||
<strong>Output:</strong> 82876089
|
||||
<strong>Explanation:</strong>
|
||||
There are 2082876103 possible text messages Alice could have sent.
|
||||
Since we need to return the answer modulo 10<sup>9</sup> + 7, we return 2082876103 % (10<sup>9</sup> + 7) = 82876089.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= pressedKeys.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>pressedKeys</code> only consists of digits from <code>'2'</code> - <code>'9'</code>.</li>
|
||||
</ul>
|
49
leetcode/problem/largest-3-same-digit-number-in-string.html
Normal file
49
leetcode/problem/largest-3-same-digit-number-in-string.html
Normal file
@ -0,0 +1,49 @@
|
||||
<p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li>
|
||||
<li>It consists of only one unique digit.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum good </strong>integer as a <strong>string</strong> or an empty string </em><code>""</code><em> if no such integer exists</em>.</p>
|
||||
|
||||
<p>Note:</p>
|
||||
|
||||
<ul>
|
||||
<li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li>
|
||||
<li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "6<strong><u>777</u></strong>133339"
|
||||
<strong>Output:</strong> "777"
|
||||
<strong>Explanation:</strong> There are two distinct good integers: "777" and "333".
|
||||
"777" is the largest, so we return "777".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "23<strong><u>000</u></strong>19"
|
||||
<strong>Output:</strong> "000"
|
||||
<strong>Explanation:</strong> "000" is the only good integer.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "42352338"
|
||||
<strong>Output:</strong> ""
|
||||
<strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= num.length <= 1000</code></li>
|
||||
<li><code>num</code> only consists of digits.</li>
|
||||
</ul>
|
Loading…
Reference in New Issue
Block a user