classSolution{public:vector<int>getRow(introwIndex){ vector<int> ans;for(inti =0; i < rowIndex +1; i++) { ans.push_back(1);for(intj = i -1; j >0; j--) ans[j] += ans[j -1]; }returnans; } };
}if(rowIndex &1) src =arr;elsesrc =arr1;for(inti =1; i <= rowIndex+1; ++i) { v.push_back(src[i]); } delete[] arr; delete[] arr1;returnv; } 最初的想法,通过两个一维数组迭代,因为当前行的值需要上一行的值,看完别人写的发现自己逗B了,其实可以发现,pascal三角形每行相比前行多一...
Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:Example 1:Input: numRows = 5 Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] Example 2:Input: numRows = 1 Output: [[1]] ...
Leetcode: Pascal's Triangle 题目: GivennumRows, generate the firstnumRowsof Pascal's triangle. For example, givennumRows= 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 1. 2. 3. 4. 5. 6. 7. 杨辉三角相信是大家再熟悉不过的啦,这里就不分析思路了! 这...
In Pascal's triangle, each number is the sum of the two numbers directly above it. Example: Input: 3 Output: [1,3,3,1] 1. 2. Follow up: Could you optimize your algorithm to use onlyO(k) extra space? class Solution: def getRow(self, rowIndex): ...
杨辉三角在英文版的LeetCode中被称为“Pascal Triangle”,这不免让人疑惑,这都是是谁的三角呢。 要不这样,谁生的早,就算谁的。 杨辉,生于1238年: Pascal(Blaise Pascal?),生于1623: OK,杨辉胜出 -- 那就叫杨辉三角。 1 读题 图源:LeetCode
sort() # dp[i] 表示第 i 个孩子分到的糖果数,初始化最少每人一个 dp: List[int] = [1] * n # ans 维护所有孩子分到的糖果数之和 ans: int = 0 #按 rating 升序进行状态转移,这样就能保证在更新 dp[i] 时, # 其左右两侧的 dp 值均已确定 for (rating, i) in cells: # 如果其评分...
0116 Populating Next Right Pointers in Each Node c++ python Medium 0117 Populating Next Right Pointers in Each Node II c++ python Medium 0118 Pascal's Triangle c++ python Easy 0119 Pascal's Triangle II c++ python Easy 0120 Triangle c++ python Medium 0121 Best Time to Buy and Sell Stock c++ ...
0116-Populating-Next-Right-Pointers-in-Each-Node/cpp-0116 0117-Populating-Next-Right-Pointers-in-Each-Node-II/cpp-0117 0117-Populating-Next-Right-Pointers-in-Each-Node-II/cpp-0117 0118-Pascals-Triangle/cpp-0118 0118-Pascals-Triangle/cpp-0118 0119-Pascals-Triangle-II/cpp-0119 0119-Pascals-Triang...
升级版本,注意 Pascal's Triangle, 其实是左右是对称的. 在子函数pascal不用全部都遍历计算,只用处理len/2,其他用对称就可以 vector<int>pascal(vector<int>&nums,intline){vector<int>rows(line+1);intlen=nums.size();rows[0]=1;for(inti=1;i<=len/2;++i){rows[i]=nums[i-1]+nums[i];rows[le...