示例 1:
示例 2:
package Array;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: IronmanJay
* @Description: 118.杨辉三角
* @CreateTime: 2022-11-22 09:03
*/
public class p118_PascalsTriangle {
public static void main(String[] args) {
List<List<Integer>> res = generate(5);
for (List r : res) {
for (int i = 0; i < r.size(); i++) {
System.out.print(r.get(i) + " ");
}
System.out.println();
}
}
public static List<List<Integer>> generate(int numRows) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
for (int i = 0; i < numRows; i++) {
List<Integer> temp = new ArrayList<Integer>();
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) {
temp.add(1);
} else {
int x = res.get(i - 1).get(j - 1);
int y = res.get(i - 1).get(j);
temp.add(x + y);
}
}
res.add(temp);
}
return res;
}
}
#include
#include
int** generate(int numRows, int* returnSize, int** returnColumnSizes)
{
int** res = (int**)malloc(sizeof(int*)*numRows);
*returnSize = numRows;
*returnColumnSizes = (int*)malloc(sizeof(int)*numRows);
for (int i = 0; i < numRows; i++)
{
res[i] = (int*)malloc(sizeof(int) * (i + 1));
(*returnColumnSizes)[i] = i + 1;
for (int j = 0; j <= i; j++)
{
if (j == 0 || j == i)
{
res[i][j] = 1;
}
else
{
int x = res[i - 1][j - 1];
int y = res[i - 1][j];
res[i][j] = x + y;
}
}
}
return res;
}
/*主函数省略*/
Java语言版
C语言版