输入:target = [1,2,3], n = 3 输出:["Push","Push","Push"]
输入:target = [1,2], n = 4 输出:["Push","Push"] 解释:只需要读取前 2 个数字就可以停止。
大致思路
模拟过程
java实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution{ public List<String> buildArray(int[] target, int n){ List<String> result = new ArrayList<String>(); int pre = 0; for(int number:target) { for(int i = 0; i < number - pre - 1; i++) { result.add("Push"); result.add("Pop"); } result.add("Push"); pre = number; } return result; } }