力扣:1441:用栈操作构建数组

This commit is contained in:
huangge1199 2021-04-01 13:04:46 +08:00
parent b9515d36d2
commit ec3d632252
2 changed files with 167 additions and 0 deletions

View File

@ -0,0 +1,105 @@
//给你一个目标数组 target 和一个整数 n每次迭代需要从 list = {1,2,3..., n} 中依序读取一个数字
//
// 请使用下述操作来构建目标数组 target
//
//
// Push list 中读取一个新元素 并将其推入数组中
// Pop删除数组中的最后一个元素
// 如果目标数组构建完成就停止读取更多元素
//
//
// 题目数据保证目标数组严格递增并且只包含 1 n 之间的数字
//
// 请返回构建目标数组所用的操作序列
//
// 题目数据保证答案是唯一的
//
//
//
// 示例 1
//
//
//输入target = [1,3], n = 3
//输出["Push","Push","Pop","Push"]
//解释
//读取 1 并自动推入数组 -> [1]
//读取 2 并自动推入数组然后删除它 -> [1]
//读取 3 并自动推入数组 -> [1,3]
//
//
// 示例 2
//
//
//输入target = [1,2,3], n = 3
//输出["Push","Push","Push"]
//
//
// 示例 3
//
//
//输入target = [1,2], n = 4
//输出["Push","Push"]
//解释只需要读取前 2 个数字就可以停止
//
//
// 示例 4
//
//
//输入target = [2,3,4], n = 4
//输出["Push","Pop","Push","Push","Push"]
//
//
//
//
// 提示
//
//
// 1 <= target.length <= 100
// 1 <= target[i] <= 100
// 1 <= n <= 100
// target 是严格递增的
//
// Related Topics
// 👍 18 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
//1441:用栈操作构建数组
public class BuildAnArrayWithStackOperations {
public static void main(String[] args) {
//测试代码
Solution solution = new BuildAnArrayWithStackOperations().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public List<String> buildArray(int[] target, int n) {
List<String> list = new ArrayList<>();
Stack<Integer> stack = new Stack<>();
int size = target.length;
for (int i = size - 1; i >= 0; i--) {
stack.push(target[i]);
}
for (int i = 1; i <= n; i++) {
if (!stack.isEmpty() && stack.peek() == i) {
list.add("Push");
stack.pop();
if(stack.isEmpty()){
break;
}
} else {
list.add("Push");
list.add("Pop");
}
}
return list;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,62 @@
<p>给你一个目标数组 <code>target</code> 和一个整数 <code>n</code>。每次迭代,需要从  <code>list = {1,2,3..., n}</code> 中依序读取一个数字。</p>
<p>请使用下述操作来构建目标数组 <code>target</code> </p>
<ul>
<li><strong>Push</strong>:从 <code>list</code> 中读取一个新元素, 并将其推入数组中。</li>
<li><strong>Pop</strong>:删除数组中的最后一个元素。</li>
<li>如果目标数组构建完成,就停止读取更多元素。</li>
</ul>
<p>题目数据保证目标数组严格递增,并且只包含 <code>1</code><code>n</code> 之间的数字。</p>
<p>请返回构建目标数组所用的操作序列。</p>
<p>题目数据保证答案是唯一的。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>target = [1,3], n = 3
<strong>输出:</strong>["Push","Push","Pop","Push"]
<strong>解释:
</strong>读取 1 并自动推入数组 -> [1]
读取 2 并自动推入数组,然后删除它 -> [1]
读取 3 并自动推入数组 -> [1,3]
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>target = [1,2,3], n = 3
<strong>输出:</strong>["Push","Push","Push"]
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>target = [1,2], n = 4
<strong>输出:</strong>["Push","Push"]
<strong>解释:</strong>只需要读取前 2 个数字就可以停止。
</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>target = [2,3,4], n = 4
<strong>输出:</strong>["Push","Pop","Push","Push","Push"]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= target.length <= 100</code></li>
<li><code>1 <= target[i] <= 100</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>target</code> 是严格递增的</li>
</ul>
<div><div>Related Topics</div><div><li></li></div></div>\n<div><li>👍 18</li><li>👎 0</li></div>