rustgym/leetcode/
_1441_build_an_array_with_stack_operations.rs

1struct Solution;
2
3impl Solution {
4    fn build_array(target: Vec<i32>, n: i32) -> Vec<String> {
5        let mut res = vec![];
6        let m = target.len();
7        let mut j = 0;
8        for i in 1..=n {
9            if j == m {
10                break;
11            }
12            res.push("Push".to_string());
13            if target[j] == i {
14                j += 1;
15            } else {
16                res.push("Pop".to_string());
17            }
18        }
19        res
20    }
21}
22
23#[test]
24fn test() {
25    let target = vec![1, 3];
26    let n = 3;
27    let res = vec_string!["Push", "Push", "Pop", "Push"];
28    assert_eq!(Solution::build_array(target, n), res);
29    let target = vec![1, 2, 3];
30    let n = 3;
31    let res = vec_string!["Push", "Push", "Push"];
32    assert_eq!(Solution::build_array(target, n), res);
33    let target = vec![1, 2];
34    let n = 4;
35    let res = vec_string!["Push", "Push"];
36    assert_eq!(Solution::build_array(target, n), res);
37    let target = vec![2, 3, 4];
38    let n = 4;
39    let res = vec_string!["Push", "Pop", "Push", "Push", "Push"];
40    assert_eq!(Solution::build_array(target, n), res);
41}