瀏覽代碼

add yanghuisanjiao

yan chuanli 2 年之前
父節點
當前提交
195aa2c420
共有 3 個文件被更改,包括 31 次插入25 次删除
  1. 0 25
      array/yang_hui_triangle.py
  2. 16 0
      array/yang_hui_triangle1.py
  3. 15 0
      array/yang_hui_triangle2.py

+ 0 - 25
array/yang_hui_triangle.py

@@ -1,25 +0,0 @@
-from typing import List
-
-
-def generate(numRows: int) -> List[List[int]]:
-    i = 1
-    res = []
-
-    while i <= numRows:
-        if i == 1:
-            res.append([1])
-            i += 1
-
-        elif i == 2:
-            res.append([1, 1])
-            i += 1
-
-        elif i > 2:
-            a = -1
-            res.append([1, i + a, 1])
-            a = i + a
-            i += 1
-
-    return res
-
-print(generate(4))

+ 16 - 0
array/yang_hui_triangle1.py

@@ -0,0 +1,16 @@
+from typing import List
+
+def generate(numRows: int) -> List[List[int]]:
+    ret = list()
+    for i in range(numRows):
+        res = list()
+        for j in range(0, i + 1):
+            if j == 0 or j == i:
+                res.append(1)
+            else:
+                res.append(ret[i - 1][j - 1] + ret[i - 1][j])
+        ret.append(res)
+
+    return ret
+
+print(generate(5))

+ 15 - 0
array/yang_hui_triangle2.py

@@ -0,0 +1,15 @@
+from typing import List
+
+def getRow(rowIndex: int) -> List[int]:
+    ret = list()
+    for i in range(rowIndex + 1):
+        res = list()
+        for j in range(0, i + 1):
+            if j == 0 or j == i:
+                res.append(1)
+            else:
+                res.append(ret[i - 1][j - 1] + ret[i - 1][j])
+        ret.append(res)
+    return res
+
+print(getRow(6))