118. 杨辉三角 Pascal's Triangle

Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,
Return

[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

题意:构造杨辉三角

  1. static public List<List<int>> Generate(int numRows) {
  2. List<List<int>> result = new List<List<int>>();
  3. for (int line = 1; line <= numRows; line++) {
  4. List<int> list = new List<int>();
  5. if (line >= 3) {
  6. list.Add(1);
  7. int index = 1;
  8. while (index < line - 1) {
  9. list.Add(result[line - 2][index - 1] + result[line - 2][index]);
  10. index++;
  11. }
  12. list.Add(1);
  13. } else if (line == 2) {
  14. list.Add(1);
  15. list.Add(1);
  16. } else if (line == 1) {
  17. list.Add(1);
  18. }
  19. result.Add(list);
  20. }
  21. return result;
  22. }





posted @ 2017-02-24 23:38  xiejunzhao  阅读(201)  评论(0编辑  收藏  举报