LeetCode: Pascal's Triangle

一点小失误,少数次过

 1 class Solution {

 2 public:

 3     vector<vector<int> > generate(int numRows) {

 4         // Start typing your C/C++ solution below

 5         // DO NOT write int main() function

 6         vector<vector<int>> ret;

 7         for (int i = 0; i < numRows; i++) {

 8             vector<int> line;

 9             for (int j = 0; j <= i; j++) {

10                 if (!j || j == i) line.push_back(1);

11                 else {

12                     vector<int> pre = ret[i-1];

13                     line.push_back(pre[j-1]+pre[j]);

14                 }

15             }

16             ret.push_back(line);

17         }

18         return ret;

19     }

20 };

 C#

 1 public class Solution {

 2     public List<List<int>> Generate(int numRows) {

 3         List<List<int>> ans = new List<List<int>>();

 4         for (int i = 0; i < numRows; i++) {

 5             List<int> line = new List<int>();

 6             for (int j = 0; j <= i; j++) {

 7                 if (j == 0 || j == i) line.Add(1);

 8                 else {

 9                     List<int> pre = new List<int>(ans[i-1].ToArray());

10                     line.Add(pre[j-1] + pre[j]);

11                 }

12             }

13             ans.Add(line);

14         }

15         return ans;

16     }

17 }
View Code

 

你可能感兴趣的:(LeetCode)