zoukankan      html  css  js  c++  java
  • 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. }





  • 相关阅读:
    ➡️➡️➡️IELTS reading by Simon on Bili
    lc0502
    lc0331
    lc0329
    lc0327
    lc0326
    lc0324
    lc0320
    lc0319
    lc0316
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/e948ec6898e9c6f816d5425666975f5a.html
Copyright © 2011-2022 走看看