最初的想法,通过两个一维数组迭代,因为当前行的值需要上一行的值,看完别人写的发现自己逗B了,其实可以发现,pascal三角形每行相比前行多一个数,那么可以通过一个数组来做,先将最后一个数右移一位,然后按公式算就行,代码如下 vector<int> getRow(introwIndex) { std::vector<int>v;inti,j;int*a=newint[ro...
classSolution {public: vector<vector<int> > generate(intnumRows) {//Note: The Solution object is instantiated only once and is reused by each test case.vector<vector<int> >res;if(numRows ==0)returnres;for(inti =1; i <= numRows; i++) { vector<int>onelevel; onelevel.clear(); on...
cls.push_back(vector<int>(1,1)); for(int i=1;i<numRows;i++) { int cur_size = (int)cls[i-1].size(); vector<int> cur_ver; cur_ver.push_back(1); for(int j=1;j<cur_size;j++) { int flag = cls[i-1][j]+cls[i-1][j-1]; cur_ver.push_back(flag); } cur_ver....
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. 杨辉三角相信是大家再熟悉不过的啦,这里就不分析思路了! 这...
杨辉三角还是 Pascal Triangle 杨辉三角在英文版的LeetCode中被称为“Pascal Triangle”,这不免让人疑惑,这都是是谁的三角呢。 要不这样,谁生的早,就算谁的。 杨辉,生于1238年: Pascal(Blaise Pascal?),生于1623: OK,杨辉胜出 -- 那就叫杨辉三角。
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]] ...
sort() # dp[i] 表示第 i 个孩子分到的糖果数,初始化最少每人一个 dp: List[int] = [1] * n # ans 维护所有孩子分到的糖果数之和 ans: int = 0 #按 rating 升序进行状态转移,这样就能保证在更新 dp[i] 时, # 其左右两侧的 dp 值均已确定 for (rating, i) in cells: # 如果其评分...
return pascalRow(rowIndex) } Javascript 解决方案 var pascalRow = function(rowIndex) { let result = []; let c = 1; result.push(c); for(let i = 1; i <= rowIndex; i++) { c = c*(rowIndex + 1 - i)/i; result.push(c); ...
0118 Pascal's Triangle Go 68.9% Easy 0119 Pascal's Triangle II Go 59.5% Easy 0120 Triangle Go 53.8% Medium 0121 Best Time to Buy and Sell Stock Go 54.5% Easy 0122 Best Time to Buy and Sell Stock II Go 63.2% Medium 0123 Best Time to Buy and Sell Stock III 44.8% Hard 012...
** Anyway, Good luck, Richardo! My code: publicclassSolution{publicList<List<Integer>>generate(intnumRows){ArrayList<List<Integer>>ret=newArrayList<List<Integer>>();if(numRows<=0)returnret;ArrayList<Integer>tri=newArrayList<Integer>();tri.add(1);ret.add(tri);for(inti=1;i<numRows;i++)...