http://oj.leetcode.com/problems/pascals-triangle-ii/
杨辉三角2,比杨辉三角要求的空间上限制Could you optimize your algorithm to use only O(k) extra space?其实计算当前行,也只用到前一行了。再前面的没有用。
class Solution { public: vector<int> getRow(int rowIndex) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. vector<int> onepiece; if(rowIndex<0) return onepiece; //1 onepiece.push_back(1); if(rowIndex == 0) return onepiece; //1 onepiece.push_back(1); if(rowIndex == 1) return onepiece; //2... vector<int> ans(onepiece); for(int row = 2;row<= rowIndex;row++) { onepiece = ans; for(int index = 1;index < row;index++) { ans[index] = onepiece[index - 1]+onepiece[index]; } ans.push_back(1); } return ans; } };