mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 15:31:43 +08:00
存量题库数据更新
This commit is contained in:
@@ -1,42 +1,58 @@
|
||||
<p>You are given an array <code>target</code> and an integer <code>n</code>.</p>
|
||||
<p>You are given an integer array <code>target</code> and an integer <code>n</code>.</p>
|
||||
|
||||
<p>In each iteration, you will read a number from <code>list = [1, 2, 3, ..., n]</code>.</p>
|
||||
|
||||
<p>Build the <code>target</code> array using the following operations:</p>
|
||||
<p>You have an empty stack with the two following operations:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>"Push"</code>: Reads a new element from the beginning list, and pushes it in the array.</li>
|
||||
<li><code>"Pop"</code>: Deletes the last element of the array.</li>
|
||||
<li>If the target array is already built, stop reading more elements.</li>
|
||||
<li><strong><code>"Push"</code></strong>: pushes an integer to the top of the stack.</li>
|
||||
<li><strong><code>"Pop"</code></strong>: removes the integer on the top of the stack.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>a list of the operations needed to build </em><code>target</code>. The test cases are generated so that the answer is <strong>unique</strong>.</p>
|
||||
<p>You also have a stream of the integers in the range <code>[1, n]</code>.</p>
|
||||
|
||||
<p>Use the two stack operations to make the numbers in the stack (from the bottom to the top) equal to <code>target</code>. You should follow the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>If the stream of the integers is not empty, pick the next integer from the stream and push it to the top of the stack.</li>
|
||||
<li>If the stack is not empty, pop the integer at the top of the stack.</li>
|
||||
<li>If, at any moment, the elements in the stack (from the bottom to the top) are equal to <code>target</code>, do not read new integers from the stream and do not do more operations on the stack.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the stack operations needed to build </em><code>target</code> following the mentioned rules. If there are multiple valid answers, return <strong>any of them</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> target = [1,3], n = 3
|
||||
<strong>Output:</strong> ["Push","Push","Pop","Push"]
|
||||
<strong>Explanation:</strong>
|
||||
Read number 1 and automatically push in the array -> [1]
|
||||
Read number 2 and automatically push in the array then Pop it -> [1]
|
||||
Read number 3 and automatically push in the array -> [1,3]
|
||||
<strong>Explanation:</strong> Initially the stack s is empty. The last element is the top of the stack.
|
||||
Read 1 from the stream and push it to the stack. s = [1].
|
||||
Read 2 from the stream and push it to the stack. s = [1,2].
|
||||
Pop the integer on the top of the stack. s = [1].
|
||||
Read 3 from the stream and push it to the stack. s = [1,3].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> target = [1,2,3], n = 3
|
||||
<strong>Output:</strong> ["Push","Push","Push"]
|
||||
<strong>Explanation:</strong> Initially the stack s is empty. The last element is the top of the stack.
|
||||
Read 1 from the stream and push it to the stack. s = [1].
|
||||
Read 2 from the stream and push it to the stack. s = [1,2].
|
||||
Read 3 from the stream and push it to the stack. s = [1,2,3].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> target = [1,2], n = 4
|
||||
<strong>Output:</strong> ["Push","Push"]
|
||||
<strong>Explanation:</strong> You only need to read the first 2 numbers and stop.
|
||||
<strong>Explanation:</strong> Initially the stack s is empty. The last element is the top of the stack.
|
||||
Read 1 from the stream and push it to the stack. s = [1].
|
||||
Read 2 from the stream and push it to the stack. s = [1,2].
|
||||
Since the stack (from the bottom to the top) is equal to target, we stop the stack operations.
|
||||
The answers that read integer 3 from the stream are not accepted.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
Reference in New Issue
Block a user