洗好的盤子摞在架子上,一個個從最上面拿,摞進櫃子。寫 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;
}
寫好了先點「執行」試試示例,沒問題再點「提交」。