洗好的盘子摞在架子上,一个个从最上面拿,摞进柜子。写 move_one(from, to):把 from 的最后一个拿出来,放到 to 的最后。from 是空的就什么也不做。两个参数都是引用,两边都会变。
stacked({"blue", "white", "red"}, 2)→['red', 'white']stacked({"blue"}, 3)→['blue']vector<string> stacked(vector<string> rack, int n)#include <string>
#include <vector>
using namespace std;
void move_one(vector<string> &from, vector<string> &to) {
// 在这里写:from 的最后一个搬到 to
}
vector<string> stacked(vector<string> rack, int n) {
vector<string> cupboard;
for (int i = 0; i < n; i++) {
move_one(rack, cupboard);
}
return cupboard;
}
写好了先点「运行」试试示例,没问题再点「提交」。