Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.983 #3134

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion solution/0100-0199/0191.Number of 1 Bits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ tags:

<!-- description:start -->

<p>编写一个函数,输入是一个无符号整数(以二进制串的形式),返回其二进制表达式中 <span data-keyword="set-bit">设置位</span> 的个数(也被称为<a href="https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E9%87%8D%E9%87%8F" target="_blank">汉明重量</a>)。</p>
<p>编写一个函数,获取一个正整数的二进制形式并返回其二进制表达式中 <span data-keyword="set-bit">设置位</span> 的个数(也被称为<a href="https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E9%87%8D%E9%87%8F" target="_blank">汉明重量</a>)。</p>

<p>&nbsp;</p>

Expand Down
6 changes: 3 additions & 3 deletions solution/0400-0499/0419.Battleships in a Board/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ tags:

<!-- problem:start -->

# [419. 甲板上的战舰](https://leetcode.cn/problems/battleships-in-a-board)
# [419. 棋盘上的战舰](https://leetcode.cn/problems/battleships-in-a-board)

[English Version](/solution/0400-0499/0419.Battleships%20in%20a%20Board/README_EN.md)

## 题目描述

<!-- description:start -->

<p>给你一个大小为 <code>m x n</code> 的矩阵 <code>board</code> 表示甲板,其中,每个单元格可以是一艘战舰 <code>'X'</code> 或者是一个空位 <code>'.'</code> ,返回在甲板 <code>board</code> 上放置的 <strong>战舰</strong> 的数量。</p>
<p>给你一个大小为 <code>m x n</code> 的矩阵 <code>board</code> 表示棋盘,其中,每个单元格可以是一艘战舰 <code>'X'</code> 或者是一个空位 <code>'.'</code> ,返回在棋盘 <code>board</code> 上放置的 <strong>舰队</strong> 的数量。</p>

<p><strong>战舰</strong> 只能水平或者垂直放置在 <code>board</code> 上。换句话说,战舰只能按 <code>1 x k</code>(<code>1</code> 行,<code>k</code> 列)或 <code>k x 1</code>(<code>k</code> 行,<code>1</code> 列)的形状建造,其中 <code>k</code> 可以是任意大小。两艘战舰之间至少有一个水平或垂直的空位分隔 (即没有相邻的战舰)。</p>
<p><strong>舰队</strong> 只能水平或者垂直放置在 <code>board</code> 上。换句话说,舰队只能按 <code>1 x k</code>(<code>1</code> 行,<code>k</code> 列)或 <code>k x 1</code>(<code>k</code> 行,<code>1</code> 列)的形状放置,其中 <code>k</code> 可以是任意大小。两个舰队之间至少有一个水平或垂直的空格分隔 (即没有相邻的舰队)。</p>

<p>&nbsp;</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Node {

<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0500-0599/0558.Logical%20OR%20of%20Two%20Binary%20Grids%20Represented%20as%20Quad-Trees/images/new_top.png" style="height: 181px; width: 777px;" /></p>

<p>如果你想了解更多关于四叉树的内容,可以参考 <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a> 。</p>
<p>如果你想了解更多关于四叉树的内容,可以参考 <a href="https://baike.baidu.com/item/%E5%9B%9B%E5%8F%89%E6%A0%91/8557650">百科</a>。</p>

<p><strong>四叉树格式:</strong></p>

Expand Down
34 changes: 15 additions & 19 deletions solution/0600-0699/0625.Minimum Factorization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,33 @@ tags:

<!-- description:start -->

<p>给定一个正整数 <code>a</code>,找出最小的正整数 <code>b</code> 使得 <code>b</code> 的所有数位相乘恰好等于 <code>a</code>。</p>
<p>给定一个正整数 <code>num</code>,找出最小的正整数 <code>x</code> 使得 <code>x</code> 的所有数位相乘恰好等于 <code>num</code>。</p>

<p>如果不存在这样的结果或者结果不是 32 位有符号整数,返回 0。</p>
<p>如果不存在这样的结果或者结果不是 32 位有符号整数,返回 <code>0</code>。</p>

<p>&nbsp;</p>

<p><strong>样例 1</strong></p>
<p><strong class="example">示例 1:</strong></p>

<p>输入:</p>

<pre>48
<pre>
<strong>输入:</strong>num = 48
<strong>输出:</strong>68
</pre>

<p>输出:</p>

<pre>68</pre>

<p>&nbsp;</p>

<p><strong>样例 2</strong></p>

<p>输入:</p>
<p><strong class="example">示例 2:</strong></p>

<pre>15
<pre>
<strong>输入:</strong>num = 15
<strong>输出:</strong>35
</pre>

<p>输出:</p>
<p>&nbsp;</p>

<pre>35</pre>
<p><strong>提示:</strong></p>

<p>&nbsp;</p>
<ul>
<li><code>1 &lt;= num &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

<!-- description:end -->

Expand Down
4 changes: 2 additions & 2 deletions solution/0700-0799/0776.Split BST/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ tags:

<!-- description:start -->

<p>Given the <code>root</code> of a binary search tree (BST) and an integer <code>target</code>, split the tree into two subtrees where one subtree has nodes that are all smaller or equal to the target value, while the other subtree has all nodes that are greater than the target value. It Is not necessarily the case that the tree contains a node with the value <code>target</code>.</p>
<p>Given the <code>root</code> of a binary search tree (BST) and an integer <code>target</code>, split the tree into two subtrees where the first subtree has nodes that are all smaller or equal to the target value, while the second subtree has all nodes that are greater than the target value. It is not necessarily the case that the tree contains a node with the value <code>target</code>.</p>

<p>Additionally, most of the structure of the original tree should remain. Formally, for any child <code>c</code> with parent <code>p</code> in the original tree, if they are both in the same subtree after the split, then node <code>c</code> should still have the parent <code>p</code>.</p>

<p>Return <em>an array of the two roots of the two subtrees</em>.</p>
<p>Return <em>an array of the two roots of the two subtrees in order</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ tags:

### 方法一:DFS

我们定义一个函数 $dfs(node)$,表示以 $node$ 为根节点的子树中,金币的超载量,即金币的数量减去节点数。如果 $dfs(node)$ 为正数,表示该子树中金币的数量多于节点数,需要将多余的金币移出该子树;如果 $dfs(node)$ 为负数,表示该子树中金币的数量少于节点数,需要将不足的金币移入该子树。
我们定义一个函数 $\text{dfs(\text{node})}$,表示以 $\text{node}$ 为根节点的子树中,金币的超载量,即金币的数量减去节点数。如果 $\text{dfs(\text{node})}$ 为正数,表示该子树中金币的数量多于节点数,需要将多余的金币移出该子树;如果 $\text{dfs(\text{node})}$ 为负数,表示该子树中金币的数量少于节点数,需要将不足的金币移入该子树。

在函数 $dfs(node)$ 中,我们首先遍历左右子树,获得左右子树的金币超载量 $left$ 和 $right$。那么当前移动的次数需要加上 $|left| + |right|$,即将左右子树中的金币移动到当前节点。然后,我们返回整个子树的金币超载量,即 $left + right + node.val - 1$。
在函数 $\text{dfs(\text{node})}$ 中,我们首先遍历左右子树,获得左右子树的金币超载量 $\text{left}$ 和 $\text{right}$。那么当前移动的次数需要加上 $|\text{left}| + |\text{right}|$,即将左右子树中的金币移动到当前节点。然后,我们返回整个子树的金币超载量,即 $\text{left} + \text{right} + \text{node.val} - 1$。

最后返回移动的次数即可。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: DFS

We define a function $\text{dfs(node)}$, which represents the coin overload in the subtree rooted at $\text{node}$, i.e., the number of coins minus the number of nodes. If $\text{dfs(node)}$ is positive, it means the subtree has more coins than nodes, and the excess coins need to be moved out of the subtree; if $\text{dfs(node)}$ is negative, it means the subtree has fewer coins than nodes, and the shortfall needs to be moved into the subtree.

In the function $\text{dfs(node)}$, we first traverse the left and right subtrees to obtain the coin overload $\text{left}$ and $\text{right}$ of the left and right subtrees, respectively. Then, the current number of moves needs to be increased by $|\text{left}| + |\text{right}|$, which means moving the coins from the left and right subtrees to the current node. After that, we return the coin overload of the entire subtree, which is $\text{left} + \text{right} + \text{node.val} - 1$.

Finally, we return the number of moves.

The time complexity is $O(n)$, and the space complexity is $O(h)$. Here, $n$ and $h$ respectively represent the number of nodes and the height of the binary tree.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,15 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Enumeration + Counting

First, we enumerate any two numbers $x$ and $y$, and use a hash table or array $cnt$ to count the occurrences of their bitwise AND result $x \& y$.

Then, we enumerate the bitwise AND result $xy$, and enumerate $z$. If $xy \& z = 0$, then we add the value of $cnt[xy]$ to the answer.

Finally, we return the answer.

The time complexity is $O(n^2 + n \times M)$, and the space complexity is $O(M)$, where $n$ is the length of the array $nums$; and $M$ is the maximum value in the array $nums$, with $M \leq 2^{16}$ in this problem.

<!-- tabs:start -->

Expand Down
Loading
Loading