mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-04 23:11:41 +08:00
update
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
<p>给你一个 <strong>二进制</strong> 字符串 <code>s</code> ,其中至少包含一个 <code>'1'</code> 。</p>
|
||||
|
||||
<p>你必须按某种方式 <strong>重新排列</strong> 字符串中的位,使得到的二进制数字是可以由该组合生成的 <strong>最大二进制奇数</strong> 。</p>
|
||||
|
||||
<p>以字符串形式,表示并返回可以由给定组合生成的最大二进制奇数。</p>
|
||||
|
||||
<p><strong>注意 </strong>返回的结果字符串 <strong>可以</strong> 含前导零。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "010"
|
||||
<strong>输出:</strong>"001"
|
||||
<strong>解释:</strong>因为字符串 s 中仅有一个 '1' ,其必须出现在最后一位上。所以答案是 "001" 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "0101"
|
||||
<strong>输出:</strong>"1001"
|
||||
<strong>解释:</strong>其中一个 '1' 必须出现在最后一位上。而由剩下的数字可以生产的最大数字是 "100" 。所以答案是 "1001" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> 仅由 <code>'0'</code> 和 <code>'1'</code> 组成</li>
|
||||
<li><code>s</code> 中至少包含一个 <code>'1'</code></li>
|
||||
</ul>
|
@@ -0,0 +1,58 @@
|
||||
<p>给你一棵 <code>n</code> 个节点的无向树,节点编号为 <code>1</code> 到 <code>n</code> 。给你一个整数 <code>n</code> 和一个长度为 <code>n - 1</code> 的二维整数数组 <code>edges</code> ,其中 <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> 表示节点 <code>u<sub>i</sub></code> 和 <code>v<sub>i</sub></code> 在树中有一条边。</p>
|
||||
|
||||
<p>请你返回树中的 <strong>合法路径数目</strong> 。</p>
|
||||
|
||||
<p>如果在节点 <code>a</code> 到节点 <code>b</code> 之间 <strong>恰好有一个</strong> 节点的编号是质数,那么我们称路径 <code>(a, b)</code> 是 <strong>合法的</strong> 。</p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>路径 <code>(a, b)</code> 指的是一条从节点 <code>a</code> 开始到节点 <code>b</code> 结束的一个节点序列,序列中的节点 <strong>互不相同</strong> ,且相邻节点之间在树上有一条边。</li>
|
||||
<li>路径 <code>(a, b)</code> 和路径 <code>(b, a)</code> 视为 <strong>同一条</strong> 路径,且只计入答案 <strong>一次</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/08/27/example1.png" style="width: 440px; height: 357px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 5, edges = [[1,2],[1,3],[2,4],[2,5]]
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>恰好有一个质数编号的节点路径有:
|
||||
- (1, 2) 因为路径 1 到 2 只包含一个质数 2 。
|
||||
- (1, 3) 因为路径 1 到 3 只包含一个质数 3 。
|
||||
- (1, 4) 因为路径 1 到 4 只包含一个质数 2 。
|
||||
- (2, 4) 因为路径 2 到 4 只包含一个质数 2 。
|
||||
只有 4 条合法路径。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/08/27/example2.png" style="width: 488px; height: 384px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 6, edges = [[1,2],[1,3],[2,4],[3,5],[3,6]]
|
||||
<b>输出:</b>6
|
||||
<b>解释:</b>恰好有一个质数编号的节点路径有:
|
||||
- (1, 2) 因为路径 1 到 2 只包含一个质数 2 。
|
||||
- (1, 3) 因为路径 1 到 3 只包含一个质数 3 。
|
||||
- (1, 4) 因为路径 1 到 4 只包含一个质数 2 。
|
||||
- (1, 6) 因为路径 1 到 6 只包含一个质数 3 。
|
||||
- (2, 4) 因为路径 2 到 4 只包含一个质数 2 。
|
||||
- (3, 6) 因为路径 3 到 6 只包含一个质数 3 。
|
||||
只有 6 条合法路径。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code></li>
|
||||
<li>输入保证 <code>edges</code> 形成一棵合法的树。</li>
|
||||
</ul>
|
@@ -0,0 +1,62 @@
|
||||
<p>给你一个长度为 <code>n</code> 下标从 <strong>0</strong> 开始的整数数组 <code>maxHeights</code> 。</p>
|
||||
|
||||
<p>你的任务是在坐标轴上建 <code>n</code> 座塔。第 <code>i</code> 座塔的下标为 <code>i</code> ,高度为 <code>heights[i]</code> 。</p>
|
||||
|
||||
<p>如果以下条件满足,我们称这些塔是 <strong>美丽</strong> 的:</p>
|
||||
|
||||
<ol>
|
||||
<li><code>1 <= heights[i] <= maxHeights[i]</code></li>
|
||||
<li><code>heights</code> 是一个 <strong>山状</strong> 数组。</li>
|
||||
</ol>
|
||||
|
||||
<p>如果存在下标 <code>i</code> 满足以下条件,那么我们称数组 <code>heights</code> 是一个 <strong>山状</strong> 数组:</p>
|
||||
|
||||
<ul>
|
||||
<li>对于所有 <code>0 < j <= i</code> ,都有 <code>heights[j - 1] <= heights[j]</code></li>
|
||||
<li>对于所有 <code>i <= k < n - 1</code> ,都有 <code>heights[k + 1] <= heights[k]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回满足 <b>美丽塔</b> 要求的方案中,<strong>高度和的最大值</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>maxHeights = [5,3,4,1,1]
|
||||
<b>输出:</b>13
|
||||
<b>解释:</b>和最大的美丽塔方案为 heights = [5,3,3,1,1] ,这是一个美丽塔方案,因为:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights 是个山状数组,峰值在 i = 0 处。
|
||||
13 是所有美丽塔方案中的最大高度和。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>maxHeights = [6,5,3,9,2,7]
|
||||
<b>输出:</b>22
|
||||
<strong>解释:</strong> 和最大的美丽塔方案为 heights = [3,3,3,9,2,2] ,这是一个美丽塔方案,因为:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights 是个山状数组,峰值在 i = 3 处。
|
||||
22 是所有美丽塔方案中的最大高度和。</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>maxHeights = [3,2,5,5,2,3]
|
||||
<b>输出:</b>18
|
||||
<strong>解释:</strong>和最大的美丽塔方案为 heights = [2,2,5,5,2,2] ,这是一个美丽塔方案,因为:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights 是个山状数组,最大值在 i = 2 处。
|
||||
注意,在这个方案中,i = 3 也是一个峰值。
|
||||
18 是所有美丽塔方案中的最大高度和。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == maxHeights <= 10<sup>3</sup></code></li>
|
||||
<li><code>1 <= maxHeights[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,62 @@
|
||||
<p>给你一个长度为 <code>n</code> 下标从 <strong>0</strong> 开始的整数数组 <code>maxHeights</code> 。</p>
|
||||
|
||||
<p>你的任务是在坐标轴上建 <code>n</code> 座塔。第 <code>i</code> 座塔的下标为 <code>i</code> ,高度为 <code>heights[i]</code> 。</p>
|
||||
|
||||
<p>如果以下条件满足,我们称这些塔是 <strong>美丽</strong> 的:</p>
|
||||
|
||||
<ol>
|
||||
<li><code>1 <= heights[i] <= maxHeights[i]</code></li>
|
||||
<li><code>heights</code> 是一个 <strong>山状</strong> 数组。</li>
|
||||
</ol>
|
||||
|
||||
<p>如果存在下标 <code>i</code> 满足以下条件,那么我们称数组 <code>heights</code> 是一个 <strong>山状</strong> 数组:</p>
|
||||
|
||||
<ul>
|
||||
<li>对于所有 <code>0 < j <= i</code> ,都有 <code>heights[j - 1] <= heights[j]</code></li>
|
||||
<li>对于所有 <code>i <= k < n - 1</code> ,都有 <code>heights[k + 1] <= heights[k]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回满足 <b>美丽塔</b> 要求的方案中,<strong>高度和的最大值</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>maxHeights = [5,3,4,1,1]
|
||||
<b>输出:</b>13
|
||||
<b>解释:</b>和最大的美丽塔方案为 heights = [5,3,3,1,1] ,这是一个美丽塔方案,因为:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights 是个山状数组,峰值在 i = 0 处。
|
||||
13 是所有美丽塔方案中的最大高度和。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>maxHeights = [6,5,3,9,2,7]
|
||||
<b>输出:</b>22
|
||||
<strong>解释:</strong> 和最大的美丽塔方案为 heights = [3,3,3,9,2,2] ,这是一个美丽塔方案,因为:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights 是个山状数组,峰值在 i = 3 处。
|
||||
22 是所有美丽塔方案中的最大高度和。</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>maxHeights = [3,2,5,5,2,3]
|
||||
<b>输出:</b>18
|
||||
<strong>解释:</strong>和最大的美丽塔方案为 heights = [2,2,5,5,2,2] ,这是一个美丽塔方案,因为:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights 是个山状数组,最大值在 i = 2 处。
|
||||
注意,在这个方案中,i = 3 也是一个峰值。
|
||||
18 是所有美丽塔方案中的最大高度和。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == maxHeights <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= maxHeights[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user