折纸

xiaoxiao2021-02-28  67

请把纸条竖着放在桌⼦上,然后从纸条的下边向上⽅对折,压出折痕后再展 开。此时有1条折痕,突起的⽅向指向纸条的背⾯,这条折痕叫做“下”折痕 ;突起的⽅向指向纸条正⾯的折痕叫做“上”折痕。如果每次都从下边向上⽅ 对折,对折N次。请从上到下计算出所有折痕的⽅向。

给定折的次数n,请返回从上到下的折痕的数组,若为下折痕则对应元素为"down",若为上折痕则为"up".

测试样例: 1 返回:["down"] class FoldPaper { public: vector<string> foldPaper(int n) { vector<string> res; foldPaper (n, "down", res); return res; } void foldPaper (int n, string val, vector<string>& res){ if(n == 0) return; //中序遍历的结果就是从上到下的折痕的数组 foldPaper (n-1, "down", res); res.push_back(val); foldPaper (n-1, "up", res); } };
转载请注明原文地址: https://www.6miu.com/read-58966.html

最新回复(0)