• LeetCode每日一题(1441. Build an Array With Stack Operations)


    You are given an integer array target and an integer n.

    You have an empty stack with the two following operations:

    “Push”: pushes an integer to the top of the stack.
    “Pop”: removes the integer on the top of the stack.
    You also have a stream of the integers in the range [1, n].

    Use the two stack operations to make the numbers in the stack (from the bottom to the top) equal to target. You should follow the following rules:

    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.
    If the stack is not empty, pop the integer at the top of the stack.
    If, at any moment, the elements in the stack (from the bottom to the top) are equal to target, do not read new integers from the stream and do not do more operations on the stack.
    Return the stack operations needed to build target following the mentioned rules. If there are multiple valid answers, return any of them.

    Example 1:

    Input: target = [1,3], n = 3
    Output: [“Push”,“Push”,“Pop”,“Push”]

    Explanation: 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].

    Example 2:

    Input: target = [1,2,3], n = 3
    Output: [“Push”,“Push”,“Push”]

    Explanation: 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].

    Example 3:

    Input: target = [1,2], n = 4
    Output: [“Push”,“Push”]
    Explanation: 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.

    Constraints:

    • 1 <= target.length <= 100
    • 1 <= n <= 100
    • 1 <= target[i] <= n
    • target is strictly increasing.

    不在 target 里的数字 push 之后马上 pop, 在 target 里的数字只 push


    
    impl Solution {
        pub fn build_array(target: Vec<i32>, n: i32) -> Vec<String> {
            let mut ans = Vec::new();
            let mut i = 0usize;
            for m in 1..=n {
                if i == target.len() {
                    break;
                }
                if target[i] != m {
                    ans.push("Push".into());
                    ans.push("Pop".into());
                    continue;
                }
                ans.push("Push".into());
                i += 1;
            }
            ans
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    leetcode309最佳买卖股票时机含冷冻期刷题打卡
    题目:2667.创建 Hello World 函数
    .NET MAUI 社区工具包 1.3版本发布
    【I/O流】从零开始学会字节流与字符流
    算法——字符串(1)
    List集合Stream流转PageInfo或Page分页
    01.Singleton Pattern 单例模式
    【紫光同创盘古PGX-Lite 7K教程】——(盘古PGX-Lite 7K开发板/PGC7KD-6IMBG256第七章)数字钟实验例程
    Python入门自学进阶-Web框架——27、DjangoAdmin项目应用-数据记录操作2
    Linux三级等保基本设置
  • 原文地址:https://blog.csdn.net/wangjun861205/article/details/127814982